1. The code

Below is the code for a simple hello world program in java.

class HelloWorld {
	
	public static void main(String args[]) {

		System.out.println("hello world");

	}
}

2. Breaking the code

It is time to break the code into pieces and look at all the pieces separately.

2.1. class

The code begins with the following line

class HelloWorld{

This line starts with a class keyword. Class keyword is used to define a class in java. A class contains all the variables and methods which define that class. After the class keyword name of the class is defined which in this case is, HelloWorld. Name of the class is used to identify a class among other classes.

<mark class="cdx-marker">Note: According to naming conventions in java name of a class should be defined in title case i.e. first letter of each word of class name should be capital as you can see in code above.</mark>

2.2. main method

After opening braces of class comes the main method.

The execution of any class starts with the main method. When a class is invoked, jre looks for the main method.

public static void main(String args[]) {

2.2.1. public

Public is the identifier used to represent that the main method is public and is accessible outside the class in which it is defined. There are other modifiers as well. Here main method needs to be public so that JRE can access it.

2.2.2. static

Static keyword is used to make the main method a class member so that it can be called without instantiating the instance of the class i.e. without creating the object of the class because it is called by the JVM before any objects are created of the class.

<mark class="cdx-marker">Note: By default all the members of a class are instance members and an object of a particular class is needed to use its instance members. Static keyword is used to define a class member.</mark>

2.2.3. Void

Void is used to tell the compiler that main method does not return anything. There are methods that return something after their execution.

2.2.4. Main

Main is the name of the method which is called when program is started.

<mark class="cdx-marker">Note: java is case sensitive so if you have typed main as Main you will get an error.</mark>

2.2.5. String args [ ]

Within the parentheses of main method all the information needed by main method is provided. These variables are called parameters. In our case there is only one parameter where String is the type of parameter and the name of parameter is args. You can name your parameter whatever you want, but always try to keep it simple. [ ] denotes that our parameter is an array which can store multiple instances of class String. This parameter will hold all the information provided to main method by command-line.

2.3. Body of main

After the signature of main method, body of main method starts with the opening curly brace all the code of main method is written in between opening and closing brace. In our case main method only contains single line of code i.e.

System.out.println("hello world");

2.3.1. System

The line begins with the System where System is a predefined class which provides access to system.

2.3.2. out

Out is the class member of system class. It is the output stream connected to the console.

2.3.3. println

Println is the method of PrintStream class which is used to generate output. Anything that is provided to println method as parameter is printed to console.  Println() ends with the a semicolon which represents the end of an statement in java.

After that comes the closing braces of method and class.

3. Summary

In this tutorial we discussed all the parts of simple hello world program written in java