How to write the hello world program in java

The Java programming process can be divided into three steps:

  • Create the program by entering the program in a text editor and saving it to the file HelloWorld.java.
  • Compile it by typing "javac HelloWorld.java" in the terminal window.
  • Execute (or run) it by typing "java HelloWorld" in the terminal window.

The program given below is the simplest Java program to print "Hello World" on the screen. Let's try to understand each piece of code step by step.

 

/* This is a simple Java program.
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[])
	{
		System.out.println("Hello, World");
	}
}

Output:

Hello, World

"Hello World!" The program consists of three main components: the HelloWorld class definition, basic methods, and source code comments. The following explanations will give you a basic understanding of code:

  1. Class definition: This line uses the class keyword to declare that a new class will be defined.
class HelloWorld 

HelloWorld is an identifier which is the class name. The entire class definition, including all its members, is between the opening brace {and the closing brace}

2. main method: In Java programming language, every application must contain a main method whose signature is:

 

public static void main(String[] args)

public: So that JVM can execute the method from anywhere.
static: Main method is to be called without object. 
The modifiers public and static can be written in either order.
void: The main method doesn't return anything.
main(): Name configured in the JVM.
String[]: The main method accepts a single argument: 
          an array of elements of type String.

Like in C/C++, primary strategy is the section point for your application and will accordingly summon the wide range of various strategies needed by your program.

3. The next line of code is shown here. Notice that it occurs inside main( ).

System.out.println("Hello, World");

This line yields the string "Hello, World" trailed by another line on the screen. Yield is really cultivated by the underlying println( ) strategy. Framework is a predefined class that gives admittance to the framework, and out is the variable of type yield stream that is associated with the control center.

4. Comments: They can either be multi-line or single line comments.

 

/* This is a simple Java program. 
Call this file "HelloWorld.java". */

This is a multiline comment. This type of comment must begin with /* and end with */. For single line you may directly use // as in C/C++.

Submit Your Programming Assignment Details