Java for students logo
Explore the English language on a new scale using AI-powered English language navigator.

Hello world program line by line

Summary

In this tutorial we will discuss Hello world program line by line and familiarize reader with the very basic principles of Java.

Code analysis

Let's recall source code for the Hello world program first:

HelloWorld.java

public class HelloWorld {

 

      /**

       * @param args

       */

      public static void main(String[] args) {

            System.out.println("Hello World! I am new to Java.");

      }

 

}

 

First of all, Java is an object-oriented programming language. Thus, Java program necessarily consists of classes. There are no global variables and functions, as you may see in C++. We called the class HelloWorld:

 

public class HelloWorld {

 

}

 

Draw attention to the fact, that filename of the file is <class name>.java. It is obligatory in Java.

Entry point

Each application in Java has an entry point to start from. It is a public static method called main. Arguments to application are passed through args parameter.

 

public static void main(String[] args) {

 

}

Print command

            System.out.println("Hello World! I am new to Java.");

 

The command above prints "Hello World! I am new to Java." to the console. We won't expect this line in depth here. At present just memorize it as a tool to output something to the console.

Comments

Above the main method declaration you see comments block:

 

       /**

       * @param args

       */

 

It is special comments, called Javadoc. Using this system, one may automatically generate documentation for properly commented source.

Build and run

Now you know all about Hello world program. In previous article we built and ran it with the click in Eclipse, but it is also useful to know, how to build applications manually.

Building

Enter the directory with your source file and run the following command:

 

       javac HelloWorld.java

After it is executed you will see a new file HelloWorld.class. This file is a compiled HelloWorld class.

Running

Now you can run it using following command:

       java HelloWorld

If you done everything right, "Hello World! I am new to Java." should appear in the console:

Hello World! I am new to Java.