Hello World Java

Hello world Java is the First Java program that we are going to create. In any programming language, the “Hello world !” is the first word that we print. It is an essential program to test the general structure of the programming language. Java is an object-oriented programming language.

In this section of learning Java programming, we will see the requirements and steps of creating a Hello World Java program. Further, we will explain each line of code.

Before proceeding further, let’s have a quick index of the topic:

  • Requirements for creating a Hello World Java Program
  • Steps For executing the Hello World Java Program
  • Code for Java Hello world!
  • Compile and Run the first Java Program
  • Explanation of the First Java Program
  • Consideration for Java Program
  • Java Hello World Program in eclipse

Requirements for Executing the Hello World Java Program

To compile and run any Java program, we must have the following tools adequately installed on our machine:

  • The JDK (Java Development Tool). See How to download and install JDK in Linux, macOS, Solaris, and Windows as per your operating system.
  • The Environment Variable Set Up
  • IDE (Integrated Development Environment) (optional). See how to download and install Eclipse IDE

Steps For executing the Hello World Java Program

The quick steps for creating and running the first java program are as following:

  1. An open Text editor ( such as Notepad for Windows, Vi for Linux)
  2. Create source code (Java program)
  3. Save the file with .java extension
  4. Open Command prompt and change to the directory where .java file exists
  5. Compile the program using javac command
  6. Run the program using java command

Step1: Open the terminal and execute the below command ( for Linux)

vi HelloWorld.java

It will open the vi editor. Move it to insert mode by pressing the ESC and I keys. It will look like:

Vi editor

For Windows, open notepad type the step2 code:

Step2:

Enter the below code in HelloWorld.java file

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

Step3: For windows, save the file as HelloWorld.java.

Step4: Open Terminal and navigate to the directory where the HelloWorld.java exists.

Step5: Execute the below command to compile “HelloWorld.java.”

javac HelloWorld.java

The above command will compile the java code and generate a HelloWorld.class file. It will not produce any output if successfully compile the code.

Step6: Now, the final step is to run the java program. Execute the below command:

Java HelloWorld

The above command will produce the below output:

Hello JavaSterling !

There may be a chance that you have made any little mistake so that you will not be able to run the program. Visit the Java primary method post; there are all possible errors listed.

Explanation of the Java program

Java is an object-oriented programming language, so the entire Java code should be in a class file. There must be the main method in a Java program, which is the public static void main(String Args[]). The Java compiler starts executing the program from the main method. The Java starting class should be public so that it can be accessible by all; otherwise, it will throw a compilation error. If we did not any access specifier by default, it would be public. Consider the below code:

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

From the above code, a class HelloWorld is declared, which is the base class of the Java program, it contains the main method and a print method. The main method is “public static void main(String[] args)” which is the entry point for the Java program.

It is public that means that anyone can access it.

It is static, which means that you can run this method without creating an instance of Main.

The void keyword stands for this method doesn’t return any value.

Next is a print statement which is used to print the string entered in it. See how it works.

Hello World Java Program in Eclipse

Many people love to code on IDE. The Eclipse IDE is one of the most popular IDEs which is used for Java programming. If you have not installed Eclipse Ide yet, see our post on How to install Eclipse IDE.

Open Eclipse IDE

Open Eclipse IDE and navigate to File->New->Java Project. It will look like:

Eclipse

Now type the name of the project as CoreFirst. Consider the below image:

Eclipse Create project

Click next to continue.

Now open the project from the project explorer menu on the left. Consider the below image:

Eclipse Project explorer

Now right click on the project name and navigate to new-> Class.

Now Enter the class name HelloWorld.java. Consider the below image:

Enter class name

Click Finish to continue. Now, Put the below code in it.

HelloWorld.java

Now, right-click on the file and select Run as ->Java Application. It will execute the first java program and produce the below output:

output