Core Java Hello World Example

This tutorial explains you how to create a simple core Java "Hello World" application. The Hello World application will print the text "Hello World" at the console. This example explains you about all the steps in creating Hello World application.

Core Java Hello World Example

This tutorial explains you how to create a simple core Java "Hello World" application. The Hello World application will print the text "Hello World" at the console. This example explains you about all the steps in creating Hello World application.

Core Java Hello World Example

Create Java Hello World Program

This tutorial explains you how to create a simple core Java "Hello World" application. The Hello World application will print the text "Hello World" at the console. This example explains you about all the steps in creating Hello World application.

In this section we will discuss about a simple Java program. A HelloWorld application explains you how to start writing of your first Java class.

Example

First we will create here a Hello World Java program then I will explain the terms what are used in this program. This is a basic example of core Java that explains how to write a Java class.

Source Code

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

Now let's see the brief description of the terms used in the above program. The important thing is to know about the Java programs is that the Java is a case sensitive programming language i.e. the text "Rose" and "rose" written in Java programs is treated differently. So, always be careful about the case sensitivity when writing the Java program. Generally the Java keywords are written into small characters.

The first line of the above program is written as "public class HelloWorld" and can be explained as below :

  • public : keyword in java. public is an access specifier that specifies the accessibility of class in a defined scope.
  • class : Java keyword used for creating a class.
  • HelloWorld : Name of class. This is a user specified name(You may follow the naming convention of class name).

In the second line a '{' (opening curly brace) is used to confine the class body.

The third line of the above program is written as "public static void main(String[] args)" and can be explained as below :

The third line of the above program is written as "public static void main(String[] args)" and can be explained as below :

This is called a main method in Java. This is a predefined method in Java and this method must be contained by every application as the signature given above.

  • public : As explained above, this is an access specifier.
  • static : A Java keyword that specifies the method (in this program) is static.
  • void : A Java keyword that specifies the method (in this program) is no returns any value.
  • main : Method name.
  • String : Predefined class of package java.lang.
  • args : User defined variable name.

The method public static void main(String args[]) can be explained as it can be accessed publicly, it is a static method and it does not return any value, the argument of this method explains that this method takes input as an array of elements of String type. The main() method specified as the signature defined above is the entry point of application and it can invoke another methods also.

In the fourth line a '{' (opening curly brace) is used to confine the body of main() method.

The fifth line of the above program is written as "System.out.println("Hello World"); and can be explained as below :

  • System : A predefined class in Java of java.lang package. This class is responsible for handling the System input, output and error of streams
  • out : Inside the java.lang.System class an object of PrintStream class is created which is responsible for output of streams.
  • println : A method of PrintStream class that is responsible for writing the output of streams.

The method System.out.println("Hello World"); can be explained as it is used for writing the output on the console.

In the sixth line a '}' (closing curly brace) is used to close the body of main() method.

In the seventh line a '}' (closing curly brace) is used to close the class body.

How to save Java program

Before saving this program first open a notepad or any other Java editor to write the above Java code and then save this file as File->Save/Save As then go to your directory where you want to save your java file and named this file as the Class name given into the program with .java extension.

How to execute Java program

Before executing the Java program a program is need to be compiled so first compile your Java program.

Open command prompt and go to your directory where you had stored your Java file then write as follows :

javac class_name.java (e.g. javac HelloWorld.java).

If no any error will be found then it will create a class file now you can execute your Java program as follows :

java class_name (e.g. java HelloWorld)

Output

Now if you compiled and execute the above Java program successfully then the output will be as follows :

Download Source Code

0