Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 04

Hello World Program in Java

In this lesson: Write your first Java program — class, main method, System.out.println — explained line by line with output. In Hindi & English.

Your first program

The classic first program in any language prints "Hello, World!". In Java it looks longer than in some languages, because every Java program must live inside a class and start from the main method. Let us write it and understand every part.

💡 In Java, all code lives inside a class, and running always starts from the main method.

Step 0: install the JDK and check it

You need the JDK (Java Development Kit), not only the JRE, because it contains the compiler javac. Install a current LTS version (for example Java 21 or 25) from Eclipse Temurin (adoptium.net) or Oracle, then open a new Command Prompt/Terminal and check both tools:

java -version
javac -version
openjdk version "21.0.12.1" 2026-08-18 OpenJDK Runtime Environment (build 21.0.12.1+1-1-24.04.4-Ubuntu) OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-24.04.4-Ubuntu, mixed mode, sharing) javac 21.0.12.1

(Output from Java 21 on Linux; your version and build text will differ.)

If you see 'javac' is not recognized on Windows, the JDK bin folder is not in PATH. Re-run the installer with “Set JAVA_HOME / Add to PATH” ticked, or add C:\Program Files\Eclipse Adoptium\jdk-21…\bin to PATH and open a new window.

The Hello World code

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

Save this as Hello.java. It looks like a lot for one line of output, but each part has a clear job, which we break down next.

Line by line

  • public class Hello — declares a class named Hello. The file must be Hello.java.
  • public static void main(String[] args) — the starting point of the program.
  • System.out.println("Hello, World!") — prints the text and moves to a new line.
  • { } — curly braces group the code that belongs to the class and the method.

Everything you want the program to do goes inside main. When you run the program, Java looks for main and starts there.

Compile and run

javac Hello.java   // compile -> creates Hello.class
java Hello         // run (no .class)
Hello, World!

javac turns your code into bytecode, and java Hello runs it. Notice you run java Hello, not java Hello.class.

Shortcut: run the source file directly

Since Java 11 you can run a single-file program without a separate compile step. Java compiles it in memory and runs it:

java Hello.java
Hello, World!

This is perfect for practice programs. For exams and real projects still learn javac + java, because bigger programs have many files.

Real error messages and what they mean

These are the exact messages the Java 21 compiler printed when we made the most common beginner mistakes:

1. Missing semicolon

Hello.java:3: error: ';' expected System.out.println("Hello, World!") ^ 1 error

The number after the file name (:3) is the line. The caret ^ points where Java expected the ;.

2. File saved as hello.java (small h)

hello.java:1: error: class Hello is public, should be declared in a file named Hello.java public class Hello { ^ 1 error

3. system instead of System

Hello.java:3: error: package system does not exist system.out.println("Hello, World!"); ^ 1 error

Java is case-sensitive: System is a class, system means nothing.

4. Main instead of main – compiles, but fails when run

Error: Main method not found in class Hello, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Terminal, IDE or online compiler?

OptionBest forNote
Terminal (javac + java)Understanding how Java really runs; board practicalsNeeds JDK + PATH
VS Code + “Extension Pack for Java”Students, light laptopsRun button, error underlines
IntelliJ IDEA Community / EclipseProjects with many files, Spring BootHeavier, more features
Online compilerPhone or a computer where you cannot installFile/class name may be fixed to Main

Newer Java: an even shorter Hello World

Java 25 added compact source files and instance main methods, so a beginner program can be written without the class line:

void main() {
    IO.println("Hello, World!");
}

It needs Java 25 or later. Most textbooks, CBSE/university practicals and interviews still use the classic public class … public static void main(String[] args) form, so learn that first.

print vs println

System.out.print("Hello ");
System.out.print("World");
System.out.println();          // new line
System.out.println("Next line");
Hello World
Next line

print stays on the same line, while println moves to a new line after printing. Use println when you want each message on its own line.

Common mistakes

  • File name mismatch. class Hello must be saved as Hello.java.
  • Missing semicolon. Every statement ends with ;.
  • Wrong main signature. It must be exactly public static void main(String[] args).
  • Running with .class. Use java Hello, not java Hello.class.

Summary

  • Every Java program lives in a class and starts from main.
  • System.out.println prints a line of text.
  • The file name must match the public class name.
  • Compile with javac File.java, run with java File.
  • print stays on the line; println moves to a new line.

Next, store data with Java variables, or set up your tools in the Java setup guide.

Frequently Asked Questions

What is the structure of a basic Java program?
Every Java program has a class, and inside it a main method where execution starts. The code you want to run goes inside main. Even a one-line program needs this class and main wrapper.
What does public static void main(String[] args) mean?
It is the starting point of a Java program. public makes it accessible, static lets it run without creating an object, void means it returns nothing, and String[] args holds any command-line inputs.
What does System.out.println do?
It prints a line of text to the screen and then moves to a new line. System.out is the standard output, and println means "print line".
Why must the file name match the class name?
Java requires the file to be named after its public class. If your class is Hello, the file must be Hello.java. Otherwise the compiler gives an error.
What is the difference between print and println?
print shows text and stays on the same line. println shows text and then moves to the next line. Use println when you want each output on its own line.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicVariables in Java Your progress: 4 / 61 (7%) · Lesson 5 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free