• Home
  • First Java Program with example

First Java Program with example

To create a Java program, you need to follow these steps:

  1. Open a text editor of your choice.
  2. Type in the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}


  1. Save the file as “HelloWorld.java”. Make sure that the file name and the class name are the same.
  2. Open a command prompt and navigate to the directory where you saved the file.
  3. Type the following command to compile the code:
javac HelloWorld.java


  1. If the code compiles successfully, you will see a “HelloWorld.class” file in the same directory.
  2. 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.