Java Hello World Program

"Hello World" - First Java Program

The introductory Java program known as the "Hello World" program is commonly the first program created by beginners. This program serves as a fundamental example to teach novices the essential components of a Java program. Implementing the "Hello World" program involves creating a class, defining the main method, and printing output to the console.

Creating a program that displays the message "Hello World" is often the initial task when starting to learn Java programming. This simple program serves as an introduction to fundamental concepts in Java.

Prerequisite to Write Hello World Program

For executing any Java program, the following software or application must be properly installed.

  • Install the JDK if we do not have installed it, download the JDK and install it.
  • Set path of the jdk/bin directory, go through the link How to Set Path in Java
  • Create the Java program.
  • Compile and run the Java program.
  • Java Hello World Program

This Java code example illustrates the process of displaying the message "Hello World" on the console:

Example

Example

public class Main{

 public static void main(String args[]){

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

 }

}

Compiling and Executing

Here are the steps for compiling and executing a Java program :

  • Save the program with a .java extension (e.g., Simple.java).
  • Open Command Prompt and navigate to the directory containing the file.
  • Compile the program using: javac Simple.java
  • Run the compiled program using: java Simple
  • View the output on the console.

javac Simple.java

java Simple

Output:

Example

Hello Java

Compilation Flow:

During the compilation of a Java program with the javac tool, the Java compiler transforms the source code into bytecode.

Parameters used in First Java Program

Let's see what is the meaning of class, public, static, void, main, String, System.out.println.

  • class keyword is used to declare a class in Java.
  • public keyword is an access modifier that represents visibility. It means it is visible to all.
  • static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it does not require creating an object to invoke the main method. So, it saves memory.
  • void is the return type of the method. It means it does not return any value.
  • The main method represents the starting point of the program.
  • String args or String args is used for command line argument . We will discuss it in coming section.
  • System.out.println is used to print statement on the console. Here, System is a class, out is an object of the PrintStream class, println is a method of the PrintStream class. We will discuss the internal working of System.out.println statement in the coming section.
  • Writing and Running a Java Program Using Notepad

In order to create a basic program, we should launch Notepad by navigating through Start Menu, then selecting All Programs, followed by Accessories, and finally clicking on Notepad. Then, proceed to write a straightforward program similar to the example provided below:

In the notepad window displayed above, a basic Java program has been authored and stored under the file name Simple.java.

To compile and execute the program provided, initiate the Command Prompt window through the subsequent instructions:

Access the command prompt by navigating through the start menu. After correctly completing the sequence: start menu -> All Programs -> Accessories -> command prompt, the corresponding window will be displayed on the screen.

To execute the program mentioned above, navigate to the current directory first. In this case, the current directory is located at c:\new. Proceed by entering the commands below:

To compile: javac Simple.java
To execute: java Simple

In how many ways we can write a Java program?

Numerous approaches exist for crafting a Java program. The alterations that can be made within a Java program are outlined as follows:

1) In Java, modifying the order of modifiers does not alter the method prototype.

Let's see the simple code of the main method.

Example

static public void main(String args[])

2) In Java arrays, the subscript notation can be positioned after the type, before the variable, or after the variable to access elements.

Let's explore various code snippets for defining the main function.

Example

public static void main(String[] args)

public static void main(String []args)

public static void main(String args[])

3) To enable var-args support for the main method, you can pass three ellipses (dots) as parameters.

Let's examine a basic example demonstrating the use of variable arguments in the main function. Further details regarding variable arguments will be covered in a subsequent section on Java's Latest Additions.

Example

public static void main(String... args)

4) In Java, it is not mandatory to include a semicolon at the end of a class declaration.

Let's see the simple code.

Example

class A{  

static public void main(String... args){  

System.out.println("hello java4");  

}  

};  //optional

Valid Java main method signature

Example

public static void main(String[] args)

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)

final public static void main(String[] args)

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

Invalid Java main method signature

Example

public void main(String[] args)

static void main(String[] args)

public void static main(String[] args)

abstract public static void main(String[] args)

Resolving an error "javac is not recognized as an internal or external command"?

In case an issue arises similar to what is shown in the illustration below, it is necessary to establish a path. Given that DOS does not identify javac and java as an internal or external command, the solution lies in configuring the Java path.

It is unnecessary to specify the path if the program is saved within the JDK/bin directory. Nonetheless, setting the path is a recommended practice. Visit this link for instructions on how to set the path in Java.

Input Required

This code uses input(). Please provide values below: