Java Hello World Program to Learn Java Programming

We will start beginning to develop a program in Java. The first program will be printing Hello World. We will learn how to create a Java program in the editor and compile and run it using command prompt.

We will show you a step-by-step process of creating Java Hello World Program. So, let us begin this tutorial.

Simple Java Hello World Program

We can simplify the process of creating and running a Java program into three steps:

1. Create the program by typing it into a text editor(like notepad). And, save it to a file named HelloWorld.java.
2. Compile the file by typing “javac HelloWorld.java” in the command prompt window.
3. To execute or run the program, type “java HelloWorld” in the command prompt window.

Print Hello World in Java

The below program is the simplest program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.

/* This is a simple Java program. 
FileName : "HelloWorld.java". */
public class HelloWorld {
  //Your program begins by calling the main(). 
  //Prints "Hello, World" to the terminal window. 
  public static void main(String args[]) {
    System.out.println("Hello World");
  }
}

Output:

Hello World

The above program consists of three primary components: the class definition, main() method and comments. The following section will give you a basic understanding of this code:

1. Class definition: We use the ‘class’ keyword to declare a new class. We can also use the access specifier like public before the class keyword:

public class HelloWorld

2. Hello World is the name of the class that is an identifier in Java. The class definition contains the members of the class that are enclosed within the curly braces{}.

3. Java Main() method: Every application in Java programming language must contain a main() method whose signature is:

public static void main(String[] args)
  • public: We declare the main method as public so that JVM can execute it from anywhere.
  • static: We declare the main method as static so that JVM can call it directly without creating the object of the class.
     Note: We can write the modifiers public and static in any order.
  • void: The main method does not return anything, therefore we declare it as void.
  • main(): main() is the name that is already configured in the JVM.
  • String[]: The main() method accepts a single argument which is an array of elements of type String.

The main method is the entry point for any Java application as in C/C++. The main() method will subsequently invoke all the other methods required by the program.

Below is the next line of code. It is present inside the main() method:

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

This line actually prints the string “Hello World” on the screen, followed by a new line on the screen. We can get the output on the screen because of the built-in println() method.

The System is a predefined class in Java. This class provides access to the system. out is the variable of type output stream that is connected to the console.

4. Java Comments: Comments in Java 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. It must begin with /* and end with */. For a single line comment, we can directly use // as in C or C++.

Important Points

  • The name of the class in the program is HelloWorld. This name is the same as the name of the file, HelloWorld.java. These same names are not a coincidence. In Java, the whole code must reside inside a class. And there must be at most one public class that contains the main() method.
  • By convention, the name of the class containing the main method should match the name of the file that holds the program.

Compiling Java Program

1. Firstly, we need to set up the environment. After that, we can open a terminal or command prompt in both Windows or Unix and can go to the directory/folder where we have saved the file: HelloWorld.java.

2. Now, to compile Java HelloWorld program, we need to execute the compiler: javac, specifying the name of the source file on the command line, like:

javac HelloWorld.java

3. The compiler creates the compiled file called HelloWorld.class in the present working directory. This class file that contains the bytecode version of the program.

Running Java Program

To execute Java program, we need to call JVM(Java Virtual Machine) using java command. After that, we specify the name of the class file on the command line, like:

java HelloWorld

This will print “Hello World” on the terminal screen.

Steps to write HelloWorld Program in Windows

Step 1: Open Command Prompt Window, Reach to the desired folder and type notepad HelloWorld.java, like this:

java hello world

Step 2: Now, hit Enter. As soon as you press enter, you will see a notepad editor screen:

Step 3: Start typing the program:

java basic programming

Step 4: Go to the File option and click the Save button to save the file. Close the notepad window. Move to the command prompt window again.

Step 5: Type here javac HelloWorld.java, and enter. If the program compiles successfully, then the cursor will start blinking on the next line, otherwise, there will be an error message.

first java program

Step 6: Now type java HelloWorld. Press Enter to get the output:

java hello world

You can see the printed output as Hello World on the screen.

Conclusion

Finally, you can start coding in Java with the editor. You just need to follow some easy steps and develop as many programs as you want. You just need to have Java installed in your system. This is the easiest way of learning programming in Java.

You can also see our article on Developing programs on Eclipse IDE in the next tutorial. We hope you may now face no difficulties with coding in Java.

Do share feedback in the comment section if you liked the article.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.