Hello World!

Certainly! The "Hello, World!" program is often the first program you write when learning a new programming language. Here's how you can create a simple "Hello, World!" program in Java:

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

Explanation:

  • public class HelloWorld: This declares a class named HelloWorld. In Java, the name of the class must match the name of the file (excluding the .java extension). In this case, the file should be named HelloWorld.java.

  • public static void main(String[] args): This is the main method. It is the entry point of the program, and it's where the program execution begins. It takes an array of strings (args) as input, which can be used to pass command-line arguments to the program.

  • System.out.println("Hello, World!");: This line of code prints "Hello, World!" to the console. The System.out.println() method is used to display text, and it automatically adds a newline character after the text, so each call to println results in a new line.

To run this program:

  1. Save the code above into a file named HelloWorld.java

  2. Open a command prompt or terminal window.

  3. Navigate to the directory where you saved the HelloWorld.java file.

  4. Compile the Java source code by running: javac HelloWorld.java

  5. After a successful compilation, run the program with the following command: java HelloWorld

  6. You should see the output "Hello, World!" displayed in the terminal.

That's it! You've created and run a simple "Hello, World!" program in Java.

Last updated