Skip to content

The first program

Implementation

Similar to all other languages it is widely known that the easiest program to create is "Hello World!". It will show only one text - exactly "Hello World" - and nothing more. You can see the example below:

package pl.sdacademy.example;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
A few words of explanation on the code above:

  • public class HelloWorld - this is a declaration of the public class HelloWorld- the topic of classes and object oriented programming will be discussed in further chapters.

  • public static void main(String[] args) - the 'main' method is the starting point of every application - this is where it all begins.

  • System.out.println("Hello World!") - this will display the text given as its argument aka ("Hello World") and then it will go to the next line - by the use of the '\n'.

Compilation and execution

If we installed the OpenJDK java compiler and installer correctly we only need to run the following commands from the Windows command line:

javac HelloWorld.java

java HelloWorld

The first command will run the program as seen in the file 'HelloWorld.java' and it will create a ready-to-go compiled code in the file HelloWorld.class.

The second command will run the application. Modern IDE will automatically run the following commands for us in the background.

As a result of executing those commands the terminal will display the following text:

Hello World!