A “Hello, World!” is a Basic program in Java that outputs Hello, World!
on the screen. Since it’s a very simple program, often used to introduce a new programming language to a newbie.
So Let’s explore how Java Hello World program.
Java programming code :
// Your First Program class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
If you have copied the exact code, you need to save the file name as HelloWorld.java
. It’s because the name of the class and filename should be the same in Java.
The output will be:
Hello, World!
The process of Running a Java program can be simplified into three steps:
- Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
- Compile it by typing “javac HelloWorld.java” in the terminal window.
- Execute (or run) it by typing “java HelloWorld” in the terminal window.
How Java “Hello, World!” Program Works?
// Your First Program
In Java, any line starting with//
means it is a comment. Comments are intended for a person reading the code and to better understand the intent and functionality of the program. It is completely ignored by the Java compiler (an application that translates a Java program to bytecode which computer can execute).class HelloWorld { ... }
In Java, every application Should begins with a class definition. In our java program, HelloWorld is the name of the class, and the class definition is:class HelloWorld { ... .. ... }
just for now remember that every Java application has a class definition, and the name of the class should match the filename in Java.
public static void main(String[] args) { ... }
This is the main method. Every application in Java must contain the main method. The Java compiler starts executing the code from the main method.public static void main(String[] args) { ... .. ... }
System.out.println("Hello, World!");
The following code prints the string Written inside quotation marksHello, World!
to standard output (your screen). Notice, this statement is inside the main function, which is inside the class definition.