Simple Program in Java

Here is an example of a simple “Hello, World!” program in Java:

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

This program consists of a single class named HelloWorld. The main method is the entry point of the program and is executed when the program is run. The System.out.println statement prints the string “Hello, World!” to the console.

To run this program, you can save the code in a file named HelloWorld.java and compile it using the Java compiler. Open a command prompt or terminal window and navigate to the directory where the file is saved, then run the following command:

javac HelloWorld.java

This will compile the code and generate a bytecode file named HelloWorld.class. To run the program, enter the following command:

java HelloWorld

This will execute the main method in the HelloWorld class and print “Hello, World!” to the console.

Add a Comment

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