Java Hello World Example


Java Hello World: Java is a simple language to practice. The code is very simple to understand and write. Many things which you are supposed to write are taken care implicitly by Java. But it takes sometime to you to accept.
Now let us write a simple example "Java Hello World" and then discuss the syntax involved.
import java.lang.*;
public class Demo
{
  public static void main(String args[])
  {
    System.out.println("Good Morning");
    System.out.println("Hello World");
  }
}

Java Hello World

Let us go line-wise explanation on "Java Hello World Example".

import java.lang.*;

import is a keyword of Java which is equivalent to include of C/C++. java.lang is known as a package. A package is equivalent to a header file of C/C++. A package is a collection of related classes and interfaces. The classes we are using in the code, String and System, belong to this package only. Java comes with many packages of which java.lang has importance. This package is implicitly imported if the Programmer does not import himself.

public class Demo

The class keyword gives name to the whole code. Here the name is Demo. All the code you write like variables, methods and constructors etc., must be placed within the open and close braces of the class. For this reason, we placed main() method in the class. It is where Java differs from C++. In C++, we place main() separately.

public static void main(String args[])

public, static and void are keywords of Java having their own meaning to the compiler. public means, main() method is accessible to any other code like JVM. static means, the main() method can be called without the help of an object by JVM. void means, the main() method does not return a value. The parameter is a String array by name args. This array is used to access command-line arguments. JVM stands for Java Virtual Machine, responsible for the execution of a Java program.

System.out.println(“Good Morning”);
System.out.println(“Hello World”);

In the above statement, System is a class, out is an object of PrintStream class and println() is a method of PrintStream class. The job of whole statement is to print at the command-prompt. It is equivalent to printf() of C-lang. One more advantage of println() is it adds a new line character (\n) automatically which must be added in C/C++ explicitly.

Note: More indepth explanation on the code "Java Hello World Example", compilation and execution is available at Basic Class Structure, Compilation and Execution

4 thoughts on “Java Hello World Example”

  1. just understood cocept of oop in java…please attach simple pgm with each concept.. and explain..after i studied this ,which topic should select to learn..

  2. i just understood cocept of oop in java…please attach simple pgm with each and explain..after i studied this ,which topic should select to learn..

Leave a Comment

Your email address will not be published.