First Java Program

Writing a Simple "Hello, World!" Program

A basic Java program prints "Hello, World!" to the console. Here is a simple example:

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

Compiling and Running Java Programs

  1. Save the file as HelloWorld.java.
  2. Compile the program using the command:
    javac HelloWorld.java
    
    This generates a HelloWorld.class file containing the bytecode.
  3. Run the program using:
    java HelloWorld
    
    This executes the compiled Java class and prints "Hello, World!" to the console.

Understanding the Structure of a Java Program

  • Class Declaration: public class HelloWorld defines the class.
  • Main Method: public static void main(String[] args) serves as the program entry point.
  • Print Statement: System.out.println("Hello, World!") prints text to the console.

This basic program introduces fundamental Java concepts and serves as a foundation for more advanced topics.