Lesson 2 of 430 minModule progress 0%

Module 1: Start Coding in Java

Understand and Change Your First Java Program

Read the minimum structure of a Java program, connect braces and statements to their roles, compare `print` with `println`, and repair several common syntax mistakes.

Author

Java Learner

Java version

Java 25 LTS

Learning goals

  • Identify the class declaration and the `main` method
  • Use indentation and braces to see which code belongs together
  • Recognize string literals, method calls, and semicolons
  • Explain the difference between `print` and `println`
  • Repair missing quotation marks, semicolons, and misplaced braces

Before you start

  • You completed the lesson about source code, the JDK, and the JVM
  • You can run a complete Java example in the browser compiler

A class contains the starter program. The line public class Main declares a class named Main. The browser compiler uses a file named Main.java, so the public class is also named Main. Java is case-sensitive: Main, main, and MAIN are different names.

The program starts in main. In these lessons, Java begins with public static void main(String[] args). You do not need to memorize the meaning of every word yet. Recognize the declaration as the entry point and place the statements that should run between its braces.

Braces define boundaries. The outer pair of braces belongs to the Main class. The inner pair belongs to the main method. A statement placed outside the method does not become part of the startup sequence.

Indentation reveals structure. Java uses braces, not indentation, to determine ownership. However, consistent indentation makes mismatched or misplaced braces much easier to notice. Indent the method inside the class and its statements inside the method.

A statement performs an action. System.out.println("Hello"); is one statement. It calls println, gives it a string value, and ends with a semicolon. The semicolon tells the compiler where the statement finishes.

Text inside double quotation marks is a string literal. The quotation marks mark the beginning and end of the text value. They are part of the Java syntax but are not printed. A missing quotation mark often causes an unclosed string error.

Java names are case-sensitive. System must begin with a capital S, while out and println are lowercase. Changing the capitalization creates a different name that Java may not recognize.

println ends the output line. After printing its value, println moves the output position to the next line. print does not. Several print calls can therefore build one line, and a final println can finish it.

Read code from the outside inward. First locate the class, then the main method, then the statements. This is easier than trying to interpret every punctuation mark at the same time.

Use compiler errors as location clues. A missing quote, semicolon, or brace can cause several messages. Begin with the first message, inspect that line and the line above it, repair one likely cause, and compile again.

Runnable examples

Read the program from the outside inward

public class Main {
    public static void main(String[] args) {
        System.out.println("Class: Main");
        System.out.println("Method: main");
        System.out.println("Action: print three lines");
    }
}

Expected output

Class: Main
Method: main
Action: print three lines

Compare `print` and `println`

public class Main {
    public static void main(String[] args) {
        System.out.print("Java ");
        System.out.print("can build ");
        System.out.println("one line.");
        System.out.println("This begins a new line.");
    }
}

Expected output

Java can build one line.
This begins a new line.

Practice the lesson

Predict first, make a change, repair a real problem, then write a small program independently.

Predict the result

Predict the exact two output lines. Pay attention to which calls use `print` and which use `println`.

public class Main {
    public static void main(String[] args) {
        System.out.print("Build ");
        System.out.print("small ");
        System.out.println("steps.");
        System.out.println("Check every result.");
    }
}
Show answer
Build small steps.
Check every result.

The first two `print` calls stay on the same line. The first `println` adds its text and then ends that line. The final `println` produces the second line.

Change the program

Turn the starter into a two-line launch message. Use at least two `print` calls to construct the first line and one `println` call for the second line.

public class Main {
    public static void main(String[] args) {
        System.out.print("Project: ");
        System.out.println("Reading Tracker");
        System.out.println("Status: ready to start");
    }
}

Success criteria

  • The output contains exactly two lines.
  • The first output line is created by more than one print statement.
  • Every statement remains inside the `main` method.
  • Every string has two quotation marks and every statement ends with a semicolon.

Repair the program structure

This program has three independent problems: an unclosed string, a missing semicolon, and one statement outside `main`. Repair all three before checking the solution.

public class Main {
    public static void main(String[] args) {
        System.out.println("Starting Java practice.);
        System.out.println("First task complete")
    }
    System.out.println("Ready for the next lesson.");
}

What happens: The compiler reports multiple syntax errors. Start with the unclosed string, then the missing semicolon, and finally move the last statement inside the `main` braces.

Show the correction
public class Main {
    public static void main(String[] args) {
        System.out.println("Starting Java practice.");
        System.out.println("First task complete");
        System.out.println("Ready for the next lesson.");
    }
}

Independent challenge

Build a small console banner that demonstrates program structure and the difference between `print` and `println`.

  • Use the standard public `Main` class and `main` method.
  • Construct the first output line with at least three `print` calls.
  • Use `println` to finish the first line.
  • Print two additional lines with `println`.
  • Indent the class, method, and statements consistently.
  • Predict the exact output before running.
Show one complete solution
public class Main {
    public static void main(String[] args) {
        System.out.print("=== ");
        System.out.print("JAVA ");
        System.out.println("START ===");
        System.out.println("Structure checked");
        System.out.println("Output checked");
    }
}

Expected output

=== JAVA START ===
Structure checked
Output checked

Common mistakes

Placing an executable statement outside the `main` method

For these starter programs, put the print statements between the opening and closing braces of `main`.

Using a lowercase class name such as `main` while the file is `Main.java`

Keep the public class name exactly `Main`, including the capital `M`.

Forgetting that quotation marks come in pairs

Check that every string begins and ends with a double quotation mark before the closing parenthesis.

Expecting several `print` calls to appear on separate lines

Use `println` when the output should move to the next line after a value is printed.

Fixing indentation but leaving a brace in the wrong place

Indentation helps you see the structure, but the actual brace positions determine which block contains the statement.

Summary

  • The public `Main` class contains the starter program.
  • Java begins in the `main` method for these examples.
  • Braces define blocks, while indentation makes those blocks easier to read.
  • Strings use paired double quotation marks, and statements usually end with semicolons.
  • `print` stays on the current line, while `println` finishes the line.

Keep the reference close

Next step

You can now read and repair the minimum program structure. Next, install Java 25 locally and confirm that the terminal is using the correct JDK.

Sources used

Advertisement

Lesson check

What is the main difference between `print` and `println`?

Next lesson →