In this Java tutorial, we will learn to write our first “Hello World” program in Java.
In any programming language, a ‘Hello World‘ program is considered a simple program which outputs Hello, World!
on the screen. It is often used to verify that the Java runtime environment is set up correctly and we are ready to start developing the real-world applications.
1. How to Print ‘Hello World’ in Java
Given Java code prints the hello world String in output console or prompt.
class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
We can copy the above program and paste it directly in the editor. Still, I will recommend you to write it yourself. This will help in understanding the difference in Syntax mainly the lower or upper case in keywords, classes, and methods.
Do not forget to save the Java file with name HelloWorld.java
. In Java, a class name and the file (in which it is written) must be the same.
When we execute the above program, we get the output in the console.
Hello, World!
2. How Java Hello World Program works?
- Java is object oriented language. Everything in Java is encapsulated inside a Java class. In this case, the class name is
HelloWorld
. - The class contains the
main()
method. It is the starting point for JVM to start the execution of the program. Remember that we must provide the exact syntax of themain
method in any Java program which we want to execute. Syntax must not be changed frompublic static void main(String[] args)
.- public means that all other class can access it.
- static means that we can run this method without creating an instance of
HelloWorld
. - void means that this method doesn’t return any value.
- main is the name of the method.
- String[] is the type which is used to refer to text content in Java. The
[ ]
brackets indicate that it is ofarray
type. - args is the name of method argument which is of type
String[]
. It means that the meain method can accept multiple text inputs while starting the program execution. These arguments are generally user inputs to program.
System.out.println
is the instruction given to JVM to print the given stringHello, World!
to console (default output target).
If there are few things, you are not able to understand then do not worry. We will learn these topics and keywords in other tutorials.
Happy Learning !!