Java Hello World Program
In this tutorial, you will learn how to write the "Hello World" program in Java.
A "Hello World" is a simple program that outputs Hello World
. It is often used to write the first program when learning a new programming language.
Let us see how Java "Hello World" program works.
Hello World using Java Programming
// First Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Output
Hello World!
How Java "Hello World!" Program Works?
// First Program
In Java, any line starting with
//
is a comment. Comments in Java are designed for users to understand the code. Comments are entirely ignored by the Java compiler (an application that translates Java program to Java bytecode that a computer can execute).public class HelloWorld { ... }
In Java, every application starts with a class definition. In the program
HelloWorld
is the name of the class, and the class definition can be given as follows:public class HelloWorld { ... }
Note: Remember, in Java, every application has a class definition, and the name of the class should match the filename. So here, the filename needs to be
HelloWorld.java
.public static void main(String[] args) { ... }
In Java, every application must contain the main method. The Java compiler searches the main method and starts executing the code from it.
We will learn the meaning of
public
,static
,void
, and how methods work in other chapters.Let us remember that the main function is the entry point of a Java application, and it is necessary for a Java program.
The signature of the main method in Java can be given as follows:
public static void main(String[] args) { ... }
System.out.println("Hello World!");
The above code output a statement. It prints the text
Hello World!
to standard output (your screen). The text inside the quotation marks is called String in Java.As we can see, the print statement is inside the main function, which is inside the class definition.
Important Points to Remember
- Every Java application must have a class definition that matches the filename.
- The main method is the entry point of a Java application.
- The main method must be inside the class definition.
- The Java compiler executes the codes starting from the main function.
The following program is a Java program that does nothing.
public class HelloWorld {
public static void main(String[] args) {
// You can write your code here
}
}