Hello World Program in Java

The “Hello World!” program in Java or any other programming languages is the first and basic program that beginners write it. The main purpose of writing this program is to understand the basic syntax and structure of the program.

Writing the hello world program in Java involves understanding concepts such as classes, methods, and the main entry point of a Java application. So, let’s write a Java program in which we will display hello world on the console.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
Hello world program in Java

Hello World Program Line-by-Line Explanation

Now, let’s break down the above Java hello world program line by line.

  • public: This is an access modifier that allows the class to be accessible from other classes.
  • class: This is a keyword used to define a class in Java. A class is a blueprint or template for creating objects.
  • HelloWorld: This is the name of the class. By convention, the name of a class in Java should start with an uppercase letter and follow CamelCase naming convention.

This is the main method, which is the entry point for executing any Java program. Let’s break it down further:

  • public: This indicates that the main method is accessible from anywhere.
  • static: This is a keyword indicating that the main method belongs to the class rather than any instance of the class. It allows the Java runtime to call this method without creating an object of the class.
  • void: This specifies that the main method does not return any value.
  • main: This represents the name of the method. It is predefined in Java and acts as the starting point of program execution.
  • String[] args: This represents an array of String objects. It is used to pass command-line arguments to the program.
  • System: This is a predefined class in the java.lang package. It provides access to system-level resources.
  • out: This is a static member of the System class and represents the standard output stream.
  • println: This is a method of the PrintStream class (to which System.out belongs) in Java. It prints the specified string on the console and then moves the cursor to a new line.
  • “Hello World!”: This is the string literal that will be printed on the console.

How to Run Program in Java?

To execute the above hello world program in Java, you should follow all the below steps:

  • Write the Code: Use a text editor or an Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code to write the Java program.
  • Save the File: Save the file with the name HelloWorld.java. The file name must match the class name.
  • Compile the Code: Open a terminal or command prompt, navigate to the directory containing the file, and run the following command:
javac HelloWorld.java

This command compiles the Java file and generates a bytecode file named HelloWorld.class, which is then executed by the Java Virtual Machine (JVM).

  • Run the Program: Execute the program by running the following command:
java HelloWorld

You should see the following output on the console:

Output:
    Hello World!

Common Errors and How to Avoid Them

1. Missing Semicolon: Every statement in Java must end with a semicolon. Forgetting this will result in a compilation error. For example:

System.out.println("Hello World!")

Error:

Error: ';' expected

2. Incorrect Class Name: The file name must match the class name. If the file is named hello.java but the class is HelloWorld, you will get an error.

Error: Could not find or load main class HelloWorld

3. Case Sensitivity: Java is a case-sensitive programming language. For instance, System must be written with an uppercase “S”. If you write it as system will cause an error.

Error: cannot find symbol

4. Missing main Method: If you do not define the main method correctly, the JVM will not know where to start the execution of program.

Error: Main method not found in class HelloWorld

These are four common mistakes that beginners often do while learning Java programming. You should remember these common errors.

Enhancements and Variations

Once you have understood how to write the basic hello world program in Java, you can make variations to enhance your understanding by the following below code.

1. Print Multiple Lines:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        System.out.println("Welcome to Java programming!");
    }
}

Now, you do this variation in your code and execute it. You will get the following output.

Output:
     Hello World!
     Welcome to Java programming!

2. Use Variables:

public class HelloWorld {
    public static void main(String[] args) {
        String message = "Hello World!";
        System.out.println(message);
    }
}

While executing this code, you will get the following output on the console.

Output:
     Hello World!

The “Hello World!” program is the simplest program in Java or any other programming languages. You should write this program and execute it because it helps you to understand the basic structure and elements involved in it. Happy coding!

FAQs on Hello World Program

1. What is the Hello World program in Java?

The “Hello World” program in Java is the simplest example used to demonstrate the basic syntax and structure of a Java program. It prints the text Hello, World! on the screen.

2. Why is Hello World the first program in Java?

It is often the first program in Java and other programming languages because it helps beginners understand how Java code works, the role of the main() method, and how to compile and run a program without complexity.

3. What are the main components of the Hello World program in Java?

The first component is class keyword to define a class. The second component is public static void main(String[] args) method which acts as the entry point. And the thrid component is System.out.println() to print output on the console.

4. How do you run a Hello World program in Java?

(a) Write the code in a file with .java extension (e.g., HelloWorld.java).
(b) Compile using javac HelloWorld.java.
(c) Run using java HelloWorld.

5. Can I write a Hello World program in Java without a class?

No, every Java program must be inside a class because Java is an object-oriented programming language.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *