Java Program Structure

Before proceed to learn Java, it is important to understand the basic structure of a Java program.

To know about the structure of a Java Program, just look at the following simple example:

/* Java Program Example - Java Program Structure
 * This is a simple Java program, prints
 * "Hello World" on the Screen
 */

public class JavaProgram
{

    /* This
     * is
     * a
     * multi-line
     * comments
     */
    
    public static void main(String args[])
    {
        
        // single line comments
        
        System.out.println("Hello World");
        
        
    }
}

When the above Java Program is compile and executed, it will produce the following output:

structure of java program

Let's explain the above Java Program:

Example

Let's look at some more Java program. Here is the first program:

/* Java Program Structure Example 1 */

public class JavaProgram
{
    public static void main(String args[])
    {
        
        System.out.println("Hello Compiler, This is simple Java program");
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java program structure

Here is another program in Java, takes input from the user:

/* Java Program Structure Example 2 */

import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        String name;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter your Name :");
        name = scan.nextLine();
        
        System.out.println("Hello " +name+ ", this is fresherearth");
    }
}

When the above program is compile and executed, it will produce the following output:

program structure of java

You can go check to many Java programs at Java Programming Examples.

Java Online Test


« Previous Tutorial Next Tutorial »