Hello World Program in Java - The Coding Shala

Home >> Learn Java >> Hello World Program in Java

How to Write Hello World Program in Java

In order to run a Java program, JRE must be installed to your system and you need a text editor to write a java program and compile it. We can use eclipse to write and run Java Programs or we can do it using notepad and a terminal window.
Hello World Program in Java - The Coding Shala
Below the given Java Program will print "Hello World" to the screen. 

/* This is a simple Java program that prints Hello World. 
   FileName : "HelloWorld.java". */
class HelloWorld 
    // Your program begins with a call to main(). 
    // Prints "Hello, World" to the terminal window. 
    public static void main(String args[])  //main method
        System.out.println("Hello World");  //use to print

Output >> 
Hello World

Let's break down this program -
The first thing the name of the class should match the name of the file that holds the program, here the class name is HelloWorld so the file name should be HelloWorld.java. Java is case-sensitive so make sure of the uppercase and lowercases.

The program begins with the following lines - 

/* This is a simple Java program that prints Hello World. 
   FileName : "HelloWorld.java". */

This is a comment. The contents of a comment are ignored by the compiler. Comments are basically used to describe the code. This is a multi-line comment that starts with '/*' and ends with '*/'.

The next line of code is - 

class HelloWorld  { ... }

Here the class keyword is used to declare the new class and HelloWorld is an identifier that is the name of the class. The entire class definition will be between the opening curly brace('{') and the closing curly brace('}').

The next line of code is - 

// Your program begins with a call to main(). 
    // Prints "Hello, World" to the terminal window. 

These are the single-line comments in java. A single-line comment starts with a '//' and ends at the end of the line.

The next line of the code is - 

public static void main(String args[])  {...}

This is the main method. In Java, every program will start executing from the main method. Here the public keyword is an access specifier that is used for the visibility of class members. When a class member is declared by the public, then that member may be accessed by the code outside the class in which it is declared. 

Note:- main() method must be declared as public because it must be called by code outside of its class when the program is started.

The keyword static allows the main() to be called without having to instantiate an instance of the class. We can access the static member without first creating a class instance.

The keyword void tells the compiler that the main() method does not return a value.

Note:- Main() is different from main().

Here String args[] is a parameter named args which is type String. The body of the main() method will be between {..}.

 The next line of code is - 
System.out.println("Hello World");

This line will print "Hello World" on the screen followed by a new line. Here println() is a built-in method that prints the output. The system is a predefined class the provides access to the system and out is the output stream that is connected to the console.

Note:- All statements in Java end with a semicolon. 

Compiling the Java Program

To compile the HelloWorld program we use javac followed by the name of the source file on the command line- 

C: \> javac HelloWorld.java

The javac compiler creates a file named HelloWord.class that contains the bytecode.  To run the java program we use Java command followed by the class name. 

C: \> java HelloWorld

This will give the output  "Hello World". 

Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.


Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala