Menu
Topics Index
...
`


Simple Java Program : Lets look at the simplest Java program which prints Hello World onto the output screen. Every Java program needs this structure and with out this the program can not be successfully executed.

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

Hello World

DESCRIPTION

Every Java program contains the following structure.


class class-name
{
    public static void main(String args[])
    {
        program-content
    }
}
In this structure(or format), only class-name, program-content and args will be different from program to program. The class-name will be the name of that program. program-content is where we include the code. This code will be executed when we run the program. args is the name of the variable to access the input parameters for the program. Although args can be called as s or arguments or input or some other name, it is a common practice to call it args or s.

In this example, we have called class-name as PrintHelloWorld and in program-content, we have included the code System.out.println("Hello World");, which will print Hello World to the screen.

The words used like class, public, static, void, main, String, System.out.println, the flower brackets {}, the parentheses () and the square brackets [], will be explained later. For now only remember the structure and understand that every program needs this structure.

THINGS TO TRY
  • Use the 'Try It' link on the top right of this program and change the print text from "Hello World" to "Hello Universe" and see the result.
  • Add one more print statement (System.out.println) to print the text "I completed my first java program." and see the output.
Please note that every program we will be discussing in Merit Campus, will contain the following sections.
  • 1. Program Name: The name of the program and few links like 'Try It'. e.g., Hello World Program
  • 2. Code: The code of the program. e.g., class PrintHelloWorld ....
  • 3. Output: The run output of the program in light green background. e.g., Hello World
  • 4. Description: A brief description explaining about the program and what it does. e.g., 'Any java program...'
  • 5. Things To Try: A list of one or more things or changes you can make to this program and observe how the program behaves. e.g., 'Change the print text..'. These suggestions can be tried using the online compiler at Try Java. Clicking on the 'Try It' link on the top right of the program will automatically take you to the 'Try Java' online compiler. Or you can download the program and compile locally using the Java Compiler (javac). The instructions for compiling programs locally are given at How to Compile and Run Java Program In Cmd Prompt.

Dependent Topics : JDK JRE JVM JIT - Java Compiler 

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.