Lesson 1 of 912 min2 challenges

Hello World and Those Five Mysterious Lines

Why Java needs so much just to print one line, and what each word is actually doing.

Five lines to print one line

Here is the smallest Java program that does anything visible.

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

It prints:

Hello, world

Python does this in one line. Java takes five. If your first reaction is "that's a lot of ceremony for one sentence" — you're right, and you're not the first person to quit over it.

So let's handle it differently. Nobody is going to tell you to "not worry about it for now." Every word up there has a job, and by the end of this page you'll know what each one does. Some of the answers are genuinely good design. One or two are honestly just history. You deserve to know which is which.

public class Main

In Java, every piece of code lives inside a class. There is no such thing as a loose statement floating at the top of a file. A class is a container with a name.

Think of a class like a labeled box. Right now you only have one box and you called it Main. Later, when you're modeling actual things — a student, an order, a bank account — each one gets its own box, and the whole idea starts earning its keep. For today it's just a container.

The hard rule: the file name must match the public class name, exactly.

Class Main means the file must be Main.java. Not main.java. Not MAIN.java. Java is case-sensitive from top to bottom, and the compiler will refuse outright:

class Main is public, should be declared in a file named Main.java

Is this rule useful, or is it history? Mostly history — it made the early compiler's job of finding classes on disk trivially easy. It stuck. You don't have to like it; you do have to match the names.

public static void main(String[] args)

This line is the front door. When you run a Java program, the JVM (the thing that executes Java) goes looking for a method with this exact signature and starts at its first line. Nothing else about your file matters to the JVM at startup — it wants this shape or nothing.

Word by word:

  • public — anyone can call this. The JVM is "anyone," so it has to be public.
  • static — callable without first creating an object. At the moment your program starts, no objects exist yet. There's nothing to call the method on. static is what makes it callable out of thin air. (Lesson 8 makes this click properly.)
  • void — this method hands nothing back when it finishes.
  • String[] args — a slot for command-line arguments. You will not use it for a long while. You also cannot leave it out, because then the signature doesn't match and the JVM won't find your front door.

Notice that main is not a keyword. It's an ordinary name that the JVM agreed to look for. Rename it to start and your code still compiles perfectly — it just never runs, because nobody knocks on a door labeled start.

The wrong mental model to drop right now: beginners often read public static void main as one magic incantation, a single unsplittable word. It isn't. It's four independent decisions that happen to appear together every time. Knowing that is what lets you read other people's Java later.

System.out.println

System is a class built into Java. out is the standard output stream inside it — your terminal. println is a method on that stream: print a thing, then start a new line.

System.out.print("A");
System.out.print("B");
System.out.println("C");
System.out.println("D");

Prints:

ABC
D

print leaves the cursor where it is. println moves to the next line after writing. So A, B, and C land on one line, and println("C") is what finally breaks the line before D.

Capitalization matters here too. system.out.println will not compile — System is a class name, and Java class names start with a capital letter.

Semicolons end statements

System.out.println("one");
System.out.println("two");

Every statement ends with ;. Java doesn't use line breaks to figure out where a statement stops — it waits for the semicolon. That's why this is perfectly legal:

System.out.println("one"); System.out.println("two");

It prints:

one
two

Two statements on one line, and Java doesn't mind. A missing semicolon is the single most common compile error you'll see this week. The message is usually ';' expected, and the line number it gives you is reliable.

Curly braces decide what belongs to what

Python uses indentation to group code. Java uses { }. Indentation in Java is purely for humans — you could delete every space and it would still compile.

Indent anyway. Always. Unindented Java is unreadable, and you will be the first person punished by it.

Braces must come in matched pairs. If you ever see:

reached end of file while parsing

you're missing a closing }. That error almost always points at the last line of your file, which is not where the problem is. Count your braces instead.

Two steps to run it

Python runs your file directly. Java compiles first, then runs.

javac Main.java    # compile: Main.java becomes Main.class (bytecode)
java Main          # run: note there is no .class here

javac reads your source and checks it — types, syntax, names — before producing anything runnable. Then java Main hands the bytecode to the JVM.

That extra step is the trade. A whole class of mistakes that Python only discovers when it reaches the broken line, Java refuses to compile at all. Misspell a variable, pass a number where text was expected, forget to return a value — javac catches it while you're still at your desk instead of at 3 a.m. in production.

You type more. In exchange, the compiler reads every line of your program before your users do. That trade is why banks and large e-commerce backends are still full of Java decades later.

Comments

// a single-line comment

/* a block comment
   that spans lines */

/**
 * a documentation comment, written above a class or method
 */

The compiler ignores all three. The third kind can be pulled out into browsable docs by tooling, which is why library code is full of it.

Ten-second check

What happens if you write system.out.println with a lowercase s?

It fails to compile. Java is case-sensitive, and System is a class in the standard library whose name begins with a capital S. The compiler will tell you it cannot find a symbol named system — which is true, there is no such thing.

Ad slot (AdSense not configured; ads appear here in production)

Hands-on

Reading it isn't knowing it. Writing it is.

0/2 passed

Challenges and grading are completely free, and a wrong answer costs you nothing. Stuck? Hit "Ask the AI": it can see the code you are writing.

Level 01Fill in the blank+10 pts · +15 XP

Complete the entry point

Fill in the two missing words so this program can start: ```java public class Main { public ____ void ____(String[] args) { System.out.println("Hi"); } } ```

Level 02Multiple choice+10 pts · +15 XP

What must the file be called?

A file contains `public class Student { ... }`. What must that file be named?

  • Might be multiple choice. Wrong answers cost nothing, so just try.

Finished? Mark it

Marking it done saves your progress, gives you 20 XP and keeps your streak alive.