Java Hello World Example

Let us start Java programming with the first simple Java program to print “Hello World”. As we already discussed in earlier tutorials, Java is an Object-oriented language so it requires writing code inside a class.

Java Hello World Example:

HelloWorld.java

package com.w3schools;

/** 
 * This program will print "Hello World".
 * @author W3schools360
 **/ 
public class HelloWorld { 
    public static void main(String args[]){ 
        //Print "Hello World". 
        System.out.println("Hello World!");
    } 
}

Output:

Hello World!

To understand the above Java program better let us have a brief look at its key points.

  1. class: is a keyword used to declare a class with a specific name.
  2. public: is an access modifier.
  3. static: is a keyword that can be used with class, method, variable/constant, or block. If it is used with a method that method is known as static method. For static method invocation no need for object creation, the method will be associated with the class.
  4. void: is the return type of the method. The void means it does not return any value.
  5. main: method is invoked by JVM, as it is static no need to create an object. It represents the program’s entry point.
  6. String args[]: represents the array of String type which is used in command line arguments.
  7. System.out.println():
    • 1. System is a final class in Java. All members of the System class are static.
    • 2. out is a public final static member of the System class. out is of PrintStream type.
    • 3. The println() is the method of the PrintStream class.

Different ways to write a Java Program

  1. The method signature will not change by modifying the sequence of modifiers. So, if we write the main method as below. Your program will run successfully.
    static public void main(String args[])
  2. You can also change the subscript notation of the String array that can be used after type, before the variable, or after the variable.
    public static void main(String[] args)  
    public static void main(String []args)  
    public static void main(String args[])
  3. Semicolon use at the end of class is optional in Java, so the program will run successfully even if you add semicolon at the end of the class.
    public class HelloWorld { 
        public static void main(String args[]){ 
            //Print "Hello World". 
            System.out.println("Hello World!");
        } 
    };

A few examples of Valid Java main() method

public static void main(String[] args)  
public static void main(String args[]) 
public static void main(String []args)  
static public void main(String[] args) 
public static final void main(String[] args)   
public static void main(String... args)  
final public static void main(String[] args)  
final strictfp public static void main(String[] args) 
 

A few examples of Invalid Java main() method

static void main(String[] args) 
public void main(String[] args)  
abstract public static void main(String[] args)   
public void static main(String[] args)  

Internal Details of Hello World Java Program

Please follow and like us:
Content Protection by DMCA.com