1.3 Parts of Java Program

Java programs are made up of different parts. We’ll begin by looking at a simple example:

/*
 This program demonstrates
 parts of a java program
 */
public class FirstProgram
{
   public static void main(String[] args)
   {
      // Display message.
      System.out.println("Hello World!");
   }
}          

Comments

The first four lines of above programs are called comments. Comments are ignored by the compiler but are useful to other programmers.

The compiler ignores everything from /* to */.

/*
 This program demonstrates
 parts of a java program
 */     

In java, single-line comments begin with // and can be placed anywhere in the line.

// Display message.

The Class Definition

public class FirstProgram
{

}

FirstProgram is the name of the Java class. Note that the file must be named to class name with .java extension. It means that this program must be saved as FirstProgram.java

The second line of the program consists of the left brace, which is matched with the second right brace (the very last brace). These braces together mark the beginning and end of (the body of) the class FirstProgram. Later we’ll discuss class in details, for now it is enough to know that every application begins with a class definition.

The main Method

In Java, every application must contain a main method whose signature is:

public static void main(String[] args)

The JVM starts running any program by executing this method first.

Finally, the line:

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

prints the characters between quotes to the console.