LearnFrenzy
Sign in Take a free test

Getting Started · Lesson 3 of 42

Your First Java Program

4 min read Updated Nov 7, 2026 Saurabh Samir

Every Java journey starts with "Hello, World!". It looks like a lot of words for one line of output, but each word has a job, and understanding them now will make the rest of the course much easier.

In this lesson

  • The structure of a Java program
  • What public static void main(String[] args) means
  • Compiling and running from the terminal
  • Printing, comments and reading compiler errors

Hello, World!

Create a file called Main.java:

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

Output

Hello, World!

Compile and run it from the folder that contains the file:

javac Main.java     # creates Main.class
java Main           # runs it (no .class at the end)

Since Java 11 you can also run a single file directly with java Main.java – handy for small programs like the ones in this course.

Every word explained

The parts of the program

  1. public class Main { … }All code lives inside a class. A public class must be in a file with the same name: Main.java
  2. public static void main(String[] args)The entry point – the JVM starts running here
  3. System.out.println("…");A statement: print a line. Statements end with a semicolon
WordMeaning
publicVisible from anywhere – the JVM must be able to call main.
staticBelongs to the class itself, so the JVM can call it without creating an object.
voidThe method returns nothing.
mainThe special name the JVM looks for.
String[] argsCommand-line arguments, as an array of strings.
System.outThe standard output stream (the console).

Interview angle

"Why is main static?" Because no object of the class exists when the program starts; a static method can be called on the class directly. "Can we overload main?" Yes, but the JVM only calls the main(String[] args) version.

Printing: println, print and printf

public class Main {
    public static void main(String[] args) {
        System.out.println("Line one");
        System.out.print("Same ");
        System.out.print("line");
        System.out.println();                    // just a new line
        System.out.println("Total: " + (40 + 2)); // + joins text and values
        System.out.printf("%s scored %d marks (%.1f%%)%n", "Asha", 92, 92.0);
    }
}

Output

Line one
Same line
Total: 42
Asha scored 92 marks (92.0%)
  • println prints and moves to the next line; print stays on the same line.
  • printf formats values: %s text, %d whole number, %.1f decimal with 1 place, %n new line, %% a percent sign.

Common mistake: "Total: " + 40 + 2

Java evaluates + left to right. Once the left side is a string, the rest is joined as text: "Total: " + 40 + 2 prints Total: 402. Wrap the maths in brackets: "Total: " + (40 + 2).

Comments

public class Main {
    /**
     * Javadoc comment: documents classes and methods.
     */
    public static void main(String[] args) {
        // Single-line comment
        /* Multi-line
           comment */
        System.out.println("Comments are ignored by the compiler");
    }
}

Output

Comments are ignored by the compiler

Rules you can't break

  • Java is case-sensitive: System ≠ system, Main ≠ main.
  • Every statement ends with ;. Blocks use { }.
  • The public class name must match the file name exactly.
  • Indentation is for humans; Java ignores it – but always indent consistently.

Reading compiler errors

The compiler tells you the file, the line and what it expected. Read the first error first – later errors are often caused by it.

public class Main {
    public static void main(String[] args) {
        System.out.println("Missing semicolon")
    }
}

Compiler output

Main.java:3: error: ';' expected
        System.out.println("Missing semicolon")
                                               ^
1 error
ErrorTypical cause
';' expectedMissing semicolon at the end of a statement
cannot find symbolMisspelt name, wrong capitalisation or missing import
class X is public, should be declared in a file named X.javaFile name and class name differ
reached end of file while parsingA missing closing brace }

Practice

Try it yourself

  1. Print your name, city and course on three lines.

    Show solution
    public class Main {
        public static void main(String[] args) {
            System.out.println("Rohan");
            System.out.println("Pune");
            System.out.println("B.Tech CSE");
        }
    }

    Output

    Rohan
    Pune
    B.Tech CSE
  2. Use printf to print "Price: ₹499.00" from the number 499.

    Show solution
    public class Main {
        public static void main(String[] args) {
            System.out.printf("Price: ₹%.2f%n", 499.0);
        }
    }

    Output

    Price: ₹499.00

Quick check

What is printed by System.out.println("A" + 1 + 2);?

A12 – after the string, + joins as text.

What must the file be called if it contains public class Invoice?

Invoice.java

Why is main static?

So the JVM can call it on the class without creating an object first.

Summary

  • Code lives in classes; the JVM starts at public static void main(String[] args).
  • Compile with javac Main.java, run with java Main (or java Main.java).
  • Use println, print and printf; watch out for string + number order.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Popular