The program in Java for displaying “Hello World”
When we begin learning a programming language, our initial task is always to display the phrase “Hello World”. In our previous article, we gained knowledge on how to install Java on Windows 10. Consequently, we are now prepared to compose and execute our primary Java program, which is the Hello World program.
Program in Java that displays the message “Hello World”
For the benefit of beginners, we have provided a basic example of a hello world program that is uncomplicated and functional.
public class JavaHelloWorldProgram {
public static void main(String args[]){
System.out.println("Hello World");
}
}
Save the program mentioned above as JavaHelloWorldProgram.java in any desired directory.
Compile and execute the Java Hello World Program.
Firstly, open the Command Prompt and navigate to the folder where the hello world program file is stored. After that, run the following commands sequentially.
$javac JavaHelloWorldProgram.java
$java JavaHelloWorldProgram
Hello World
If you are running Java 11 or a newer version, you can directly run the command java JavaHelloWorldProgram.java to compile and execute the program without the need to separately compile and run the Java program.
2. Key aspects of a Java Program
-
- In Java, a source file can contain multiple classes, but only one of them can be declared as public. It is important for the name of the Java source file to match the name of the public class. That’s why our program file is named JavaHelloWorldProgram.java.
When we compile the code, it is converted into byte code and saved with a .class extension, using the name of the class. If you check the directory where we compiled the Java file, you will find a new file called JavaHelloWorldProgram.class.
When we execute the class file, we only need to provide the public class name, without the entire file name.
To run the program using the java command, the class is loaded into the Java Virtual Machine (JVM) which searches for the main method in the class and executes it. The syntax of the main function must match the specifications in the program, otherwise it will not run and throw an exception with the message “Exception in thread “main” java.lang.NoSuchMethodError: main”.
I recently made a brief video demonstrating how to create a Java Hello World Program using both Notepad and Eclipse. You can watch the video on YouTube for a clearer understanding: https://www.youtube.com/watch?v=ZREpFyjTDho That’s all for now, and you can now begin experimenting with your first class. In my next post, I will delve deeper into classes, JDK, JVM, and other features offered by the Java programming language. Additionally, don’t forget to check out this post for a comparison between JDK, JRE, and JVM in Java.
more tutorials
Tutorial on Java Server Faces (JSF)(Opens in a new browser tab)
jshell for interactive Java programming(Opens in a new browser tab)
jshell for interactive Java programming(Opens in a new browser tab)
Spring WebFlux the Spring Reactive Programming(Opens in a new browser tab)
Spring Boot CLI(Opens in a new browser tab)