Hello World Java Program

In the previous tutorial, we saw how to set up a java environment. If you did not set your java environment till now then go to this tutorial -> Java Environment Setup before doing any program in java. Now let start to do Hello World Java Program.

What step you will follow for creating a java program

  • Download any editor like notepad++ or simply use notepad of your computer.
  • Do coding on the editor.
  • Save the file with the same class name on any location.
  • Go to command prompt then Compile and run the java program.

Hello World Example

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

output:

Hello World

Program explained

In our example, we named the class  HelloWorld. A class should always start with an uppercase letter. In Java, all code is placed inside the class.

The name of the java file must have the same as the class name. Please note that if you saving the file you must have to save it with the same name of the class and will place the .java at the end of its name.

main Method

In every Java program, The main() method is required. Without the main method, you can not run any program.

public static void main(String[] args)

public: It is an access specifier and the public is a keyword that is used for publicly visible to JVM. Because JVM can identify the execution point of the program.

static: It is a keyword. Static keyword used before any methods and variables and can be directly accessed with the help of Class, which means that there is no need to create objects.

void: Every method in java has the return type. If you do not return any value then simply write void keyword at the starting of the method name.

main(): main is the method name and It’s fixed and when we start a java program, it looks for the main method.

String args[]: It is used for accepts data from the user. It is used for accepts data from the user. It also used to accepts a string array.

System.out.println(): is used to display the message in the console which is written inside the print() method.

How to compile and run a java program

First, you open the notepad on your machine. Then write Hello World java program like this.

hello-world-java-program

Now, its time to compile and run a java program. First type javac HelloWorld.java on command prompt then press enter.

After press enter, your program compiled successfully if not found any error in your program. Now type java HelloWorld and then press enter. After this, you will see the output in your command prompt like this.

hello-world-java-program-2
Do you know?
Hello World Java Program This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.