Java Hello World Program: Java programming is the high-level, Common Programming Language that was developed by Sun Microsystems in the mid-1990s before Oracle Corporation acquired it. It has been built on the principle of “writing once, running anywhere” WORA, meaning that Java code can be written in one platform and executed anywhere without recompilation. This feature makes Java programs as very portable and flexible.
Due to the platform’s independence and many libraries and frameworks, Java has proven to be an extremely popular language in the last few years. It is widely used to create a wide range of applications, e.g., desktop software for the corporate world, enterprise-level web applications, mobile apps, and embedded systems.
If you are an individual, who is just starting out with coding. It is essential to learn the Java Hello World program to make a strong base of the programming concepts. Today, in this article we will be understanding everything about how to write a Java Hello World program, where to write it, and how it will help you in your further journey.
What Is Java Hello World Program?
A Java Hello World program is the most basic program written to introduce beginners to Java programming. It consists of a simple code that prints the message “Hello, World!” on the screen. This program helps new learners to understand the structure and syntax of Java, such as defining a class, using the `main` method, and printing output using `System.out.println()`. It’s a great starting point for those who are just beginning their journey in Java programming.
Basic Steps To Follow To Write Java Hello World Program
Before starting your journey in the Java programming language, It is necessary to learn about some of the basic steps that you will follow to implement the Java hello world program. These basic steps include:
- To write the program.
- Compiling the program.
- To run the program.
Important Terms Used In Java Hello World Program
To have your basics cleared and for your better understanding, here is a simple guide to all the terms that are going to be used in the Java Hello World program. Understanding these important ters will help you to understand the Java program in a better way:
- In Java, the `class` keyword is used to create a class.
- The `public` keyword is an access modifier that controls visibility. It means the class or method can be accessed from anywhere.
- The `static` keyword is used to declare a static method. The advantage of a static method is that it does not require an object to be called. For example, the `main()` method is static, so the Java Virtual Machine (JVM) can run it without creating an object, saving memory.
- The `void` keyword indicates that the method does not return any value.
- The `main` method is the entry point of any Java program.
- `String[] args` or `String args[]` is used to pass command-line arguments to the program. We will discuss it further in a later section.
- `System.out.println()` is used to print a message on the screen. We will explore how all these terms work internally and integrally in another section.
Steps to Create a Java Program
Step 1: Install and Set Up Java Development Kit (JDK)
The first step in creating a Java program is to install the Java Development Kit (JDK) on your computer. The JDK is a software package that provides everything you need to write, compile, and run Java programs. You can download it from the official Oracle website.
After installing, you need to set up the Java environment by configuring the system’s `PATH` variable, which ensures that Java commands can be executed from any directory on your computer.
Step 2: Write the Java Program in a Text Editor
Once the JDK is installed, open any text editor, like Notepad in Windows or any IDE such as Eclipse or IntelliJ IDEA. In the editor, you will be writing the Java code. A basic simple Java Hello World program example is given below for reference:
Java Hello World Program |
public class HelloWorld {
public static void main(String[] args) { System.out.println(“Hello, World!”); } } |
In this step, you are defining a class named `HelloWorld` and adding a `main()` method, which will print the message “Hello, World!” to the screen. While doing this, please ensure that the class name matches the filename exactly.
Step 3: Save the Program with a `.java` Extension
After writing the program, save the file with the `.java` extension. For this example, you would save it as `HelloWorld.java`. Make sure that the filename matches the class name you wrote in the code. It is important to save the file in a location where you can easily access it later for compilation and execution.
Step 4: Compile the Java Program
To compile the program, open the command prompt in windows or terminal in Mac. Navigate to the directory where you saved the `HelloWorld.java` file using the `cd` command. Once in the correct directory, type the following command written below and press Enter:
Command For Compilation Of Java Hello World Program |
javac HelloWorld.java |
This command uses the Java compiler (javac) to convert your human-readable Java code into bytecode, which the JVM can understand. If there are no errors in your code, a file named `HelloWorld.class` will be created in the same directory.
Step 5: Run the Java Program
After the program is successfully compiled, you can run it by typing the following command in the command prompt or terminal:
Command For Running Java Hello World Program |
java HelloWorld |
This command tells the Java Virtual Machine (JVM) to execute the bytecode in the `HelloWorld.class` file. The program will run, and you should see the output as:
Output |
Hello, World! |
This means your program has successfully printed the message to the screen. Congratulations! You’ve just written and executed your first Java program.
Points to Remember While Writing a Java Hello World Program
There are three steps to simplifying the Java programming process: To create a program, you can type it in your text editor, then save it to the folder Hello World.java. Type ‘ Hello World JAVA ‘ to compile it in the terminal window. You can run it by typing Hello World Java in the terminal window:
- The HelloWorld class has the same name as the fileHelloWorld.java specified by this program. That is not a coincidence. All codes are required to be in the class within Java, and there’s usually at least one public class containing a main() method.
- In general, the name of a main class, “class” which contains this basic method, should be matched to that file holding the program.
- The class definition for each Java program should be identical to the name of the filename and its file name and class name should be same.
Learn Java Programming with PW Skills
Join our Java+ DSA course to learn the basics and fundamentals of the Java language along with the important data structure. Our course is provided by industry level experts to help you become efficient in coding with Java. Candidates can clear any of their doubt with our 1:1 doubt sessions. Also, we will provide candidates with 100% placement assistance.
Java Hello World Program
Q1. How do you write JAVA Hello World in Java?
Ans. class HelloWorld public static void MainString[]) args System. out. println 'Hello World!' //helloworld. 'Hello World' will be displayed when this code is run.
Q2. How to write the first program in Java?
Ans. In order to simplify the programming process in Java, we have broken it down into three steps:
You can create the program by selecting it in a text editor and saving it to your own folder, e.g. MyProgram. Java.
Compile "javac MyProgram. java" in your terminal window.
Use "MyjavaProgram" in the Terminal window to enable or disable it.
Q3. What are the basics of Java syntax?
Ans. Java Syntax's an essential language, all the rules, commands, and patterns of writing programs that computers and compilers understand. The syntax of every programming language is the same as that of the human language.