To create a Java program, you need to follow these steps:
- Open a text editor of your choice.
- Type in the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
- Save the file as “HelloWorld.java”. Make sure that the file name and the class name are the same.
- Open a command prompt and navigate to the directory where you saved the file.
- Type the following command to compile the code:
javac HelloWorld.java
- If the code compiles successfully, you will see a “HelloWorld.class” file in the same directory.
- Type the following command to run the program:
java HelloWorld
You should see the output “Hello, World!” displayed on the command prompt.
Explanation of the code:
- The “public class HelloWorld” line declares a class named “HelloWorld”.
- The “public static void main(String[] args)” line is the main method of the class. This is the entry point for the program.
- The “System.out.println(“Hello, World!”);” line prints the string “Hello, World!” to the console.
- The “} }” lines mark the end of the main method and the end of the class.