java


Java Program Structure and First Hello World

When you start learning Java, the first step is to understand the basic structure of a program. Just like a building needs a blueprint, every Java program follows a specific structure that the Java compiler can understand. Once you know the structure, you can easily write and run your first program — the famous "Hello, World!".

 Structure of a Java Program
A simple Java program typically consists of:

Package Statement (optional)

Used to define the package (folder-like structure) where your class belongs.

Example:
package mypackage;
Import Statements (optional)

Used to bring in built-in or custom classes from other packages.

Example:
import java.util.Scanner;
Class Definition (mandatory)

Every Java program must have at least one class.

The filename must match the class name (case-sensitive).

Example:

public class HelloWorld {
    // class body
}
Main Method (mandatory for standalone execution)

The entry point of every Java program.

Must be written exactly as:

public static void main(String[] args) {
    // code goes here
}
Statements

Actual instructions you want the program to execute.

Example:

System.out.println("Hello, World!");
  

First Java Program: Hello World

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

Step-by-Step Explanation

public class HelloWorld Defines a class named HelloWorld. The public keyword means it’s accessible from anywhere. public static void main(String[] args) The starting point of the program. public → accessible from anywhere. static → can run without creating an object of the class. void → does not return any value. String[] args → command-line arguments. System.out.println("Hello, World!"); Prints text to the console. System → a built-in class in Java. out → standard output stream. println() → prints text and moves to the next line.


How to Run Your First Program

Step 1: Save the file as HelloWorld.java. Step 2: Open terminal/command prompt and navigate to the file’s location. Step 3: Compile the program: javac HelloWorld.java Step 4: Run the program: bash java HelloWorld Output: Hello, World!


Key Points to Remember File name must match the public class name.

Java is case-sensitive (HelloWorld and helloworld are different). main method is the entry point of the program.


Use System.out.println() to print output.

✅ With this, you’ve learned how a Java program is structured and written your first Hello World. From here, you can move on to variables, data types, and more complex logic.