Java - Hello World Program

Java

In this tutorial we will learn about the basic structure of a Java program.

Open your text editor or IDE and lets write the famous "Hello World" program.

Once done, save the file by the name HelloWorld.java.

class HelloWorld {
  public static void main(String args[]) {
    System.out.println("Hello World");
  }
}

Lets talk about the program line by line.

The HelloWorld class

class HelloWorld {
	
}

Java is an object oriented programming language and everything we write must exists inside a class.

Inside the HelloWorld.java file we have created a class by the name HelloWorld.

This line is a class definition and it tells the compiler that a class is defined.

Note! class is a Java keyword and holds a special meaning. We will learn about keywords in the coming tutorials.

We also have the opening and closing curly brackets and they mark the starting and ending of the class.

The main method

public static void main(String args[]) {
	
}

This is the main method of the HelloWorld class.

This is similar to the main() function of C programming language.

Every Java program needs a main method as it marks the starting point for the Java interpreter to start the program execution.

And the main method also has the opening and closing curly brackets to mark the starting and ending of the method.

There are three keywords used in the main method line which are discussed below.

public

This is the access specifier and it tells us that the main method is public and is accessible from other classes.

static

The static keyword makes the main method belong to the class and not to any object of the class.

It is important to declare the main method as static as the interpreter will use this method before any object is created.

void

This is the return type for the main method and it means that the main method will not return any value.

Then we have the String args[] parameter in the main method. which means we are creating an array variable by the name args and its data type is String.

The System.out.println

System.out.println("Hello World");

This is similar to the printf function of the C programming language.

So, we are using the println() method which is a member of the out object, which is a static member of the System class.

And we are using this println() method to print the text Hello World.

The semicolon

The ; semicolon marks the end of the statement.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT