Let’s start with Understanding The First Java Program to Print Hello world in Java.
A basic Hello world program in Java, that displays “Hello, World!”. It’s mostly used to illustrate the syntax of the language.

Output

Learn how to Write, Compile & Run a Java Program by learning this Tutorial :→ Developing Java Programs

 

Understanding The First Java Program

First of all we must remember that java is a highly case sensitive
language.

It means that we have to be very careful about uppercase and lowercase letters while typing the code.

In our very first java program three letters are compulsorily in uppercase and they are
“T” of Test ,
“S” of String and
“S” of System.

This is because in java class names begin with upper case

→ The first statement of our code is:

class Test

Since java is an Object Oriented Language and it strictly supports Encapsulation so every java program must always contain at-least one class and whatever we write must appear within the opening and closing braces of the class

→ The second statement of our code is:

public static void main(String [ ] args)

In java also(like C/C++) the entry point of execution of our program is the main( ) method which is called by the JVM.

The words shown in pink are keywords and each has a different meaning and purpose for the method main( ).

Lets understand each of them in detail

Why main( ) is public ?

→ “public” is an access modifier and in Object Oriented Programming any method or variable which is declared public can be accessible from outside of that class.

→ Since main( ) method is public in Java, JVM can easily access and execute it.

Why main( ) is static ?

→ Every class can have two kinds of methods , nonstatic and static.

→ A method which is nonstatic can only be called using object of that class , while a static method can be called without any object , simply using class name.

→ When the JVM makes are call to the main( ) method there is not object existing for the class being called therefore it has to have static method to allow invocation from class.

Why main( ) has return type void ?

→ The keyword void is called return type which indicates that no value will be returned by the method to it’s caller.

→ Since main() method in Java is not supposed to return any value to the JVM, its made void which simply means main() is not returning anything.

Can we change/remove the keywords used with main () ?

→ No , not at all.

→ This is because main( ) is called by JVM and to allow JVM to successfully call main( ) these keywords are important.

→ If we forget to write these keywords then although the code will compile but will fail to run.

All we can do is change the order of public and static but we can’t drop them

What is String { } args ?

String is a predefined class in java

→ So the statement String [ ] args is declaring args to be an array of Strings.

→ It is called command line argument and we will discuss it later.

→ For now, just remember that the statement String[ ] args has to be present with main( ) otherwise code will not run

Can we change/drop String { } args ?

→ No , just like keywords used with main are compulsorily , similarly String [ ] args is also compulsory.

→ All we can do is change the name from args to something else.

→ Also we can interchange the array name and [ ] .

For example: String args[ ], String [ ] args, String [ ]str,String str[ ] all are valid

Understanding System.out.println( )

→ Now let’s understand code in the body of the main( ) method, which will print a message on the console.

Syntax: System.out.println(“ Hello World ”);

System is a predefined class .

out is an object reference (not object).

println( ) is a method.

→ Together all three are used for displaying text on console.

→ We will discuss this part in detail once we have covered basics of java

Understanding The First Java Program