Skip to main content
Documents
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

First Java Program: Hello World

Creating your first Java program is an essential step in learning the language. The "Hello, World!" program is a simple yet fundamental example that demonstrates the basic structure of a Java application. This guide will walk you through writing, compiling, and running your first Java program.

Writing Your First Java Program

To create a "Hello, World!" program, follow these steps:

  1. Open a Text Editor: You can use any text editor, such as Notepad, TextEdit, or an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.
  2. Write the Code: Type the following code into your text editor:

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

Explanation of the Code

  • public class HelloWorld: This line declares a public class named HelloWorld. In Java, every application must have at least one class definition.
  • public static void main(String[] args): This is the main method, the entry point for any Java application. The main method is always required in a standalone Java application.
  • System.out.println("Hello, World!");: This line prints the text "Hello, World!" to the console. System.out is a standard output stream, and println is a method that prints a line of text.

Compiling the Program

Before running your program, you need to compile it:

  1. Save the File: Save your file with the name HelloWorld.java. The filename must match the class name and be case-sensitive.
  2. Open Command Prompt/Terminal: Navigate to the directory where your HelloWorld.java file is saved.
  3. Compile the Program: Enter the following command to compile your program:

javac HelloWorld.java

This command uses the Java compiler (javac) to convert your Java code into bytecode, generating a HelloWorld.class file.

Running the Program

After successful compilation, run your program with the following command:


java HelloWorld

If everything is set up correctly, you should see the output:

Hello, World!

Tips and Best Practices

  • Case Sensitivity: Java is case-sensitive. Ensure that you use the correct capitalization for class names, method names, and file names.
  • File Naming: The filename must match the public class name exactly, including capitalization.
  • Environment Setup: Ensure that Java Development Kit (JDK) is installed and configured correctly on your system. You can verify the installation by running java -version and javac -version in your command prompt or terminal.
  • Use Comments: Add comments to your code to improve readability and maintainability. For example:

// This is a single-line comment
  • Consistent Formatting: Follow consistent code formatting practices for better readability. Many IDEs offer automatic code formatting features.

Learn Java Essentials

Build your Java skills from the ground up and master programming concepts.
Start Learning Java for Free