Sonntag, 5. Juli 2015

Java - hello world

This blog is for absolutly programming beginners starting with Java. It shows the typical "hello world" example Java code.

There are many exmples out there. There are maybe some unknown keywords. They will be explained as well for a better undestanding about what happens under the hood.

public class JhelloWorld
{
    public static void main(String[] args)
    {
        System.out.print("Hello world!");
    }
}


Script 1: JhelloWorld.java

As beginner its sometimes confusing where to start understanding the first or a new programming language. The concepts behind are simple once they are understood.

First have a look at JhelloWorld (although its not the start of the script). This is the name of the class!

Java is used for object oriented programming. Before generating any objects, there must be a class first. Imagine there is an engineering detail drawing of a car. This is comparable to a class in Java. The built (real) cars are the objects of the drawing.

The keyword class notated before JhelloWorld must be a class. The first step is done! :)

The keyword public grants access to the code within the class. In most languages there is a scope to ensure security within a code or a defined part of it. You can define further data as private or protected, to ensure only the class logic itself is capable to calcuate changes (i.e. a tax amount calculation in business logic is a serious thing).

Inside the class braces { } there can be data and methods. What is it used for? To go back to the car example, data can be the horspower. A method can be steer(). This is a general concept of Object Oriented Programming (OOP). The data should only be changed with the object methods, no direct access. Direct access is possible but not recommended.

In the JhelloWorld there is only the method main(String[] args). The main method will be called automatically if the program is executed on the operating system. Between the OS and the Java executable there is usually a JVM - Java Virtual Machine. For each OS (Windows, Linux, ...) are JVMs who care about memory, OS undependent executable programms and some other things.

The keyword public can also be used for methods, if it should be callable outside of the class.

The keyword static defines there must be no object generated to get direct access to further data or method of the class. There is nothig I could compare with the car example. If I get a clue I'll let you know.

The keyword void defines the method main() provides no data. It can be called, there might be data operation in the background but there is no output of the method itself. Methods are seen as [input] > [method-process] > [output] operations. void simply means there is no answer of the method to the caller. To give you an example with an integer number of type int, have a look at Script 2: return value. With void there is nothing to return with (because its void).

void caller()
{
    int number = 0;        // number is '0'
    
    number = getNumber();  // number changed from '0' to '5'
}

int getNumber()
{
    return 5;              // returns always '5'
}

Script 2: return value

String[] args consists of three parts. String is a datatype, it can only represent text (no values, objects, ...). The square bracket [] defines a 1-dimensional list called array. Imagine a tablesheet with only 1 row in a line. The args is a variable. It can hold the previous explained stringarray. The argument is used in C/C++ as well, to pass information to a programm in a shell or the command line tool. If you create an executable programm called sum which calculates the sum of two values, it can be called like this.

C:\> sum 2 5
7


The command System.out.prinln("Hello world!)"; prints the text "Hello world!" to the shell or command line tool where the program is called from.

An example to call the sample, go into the folder, be sure you have setup Java correctly on your system and type

C:\workspace\JhelloWorld\bin> java JhelloWorld
Hello world!

Keine Kommentare:

Kommentar veröffentlichen