Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Comments · Lesson 23 of 28

Best Practices

A free lesson from Swipe Coding · Java, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
You know the syntax — now for writing GOOD comments. ✍️

You now know both types of comments in Java: // and /* */. But just as important as the syntax is knowing when and how to use comments effectively. Here are the most important rules for good comments.

Rule 1: Explain the WHY, not the WHAT. Good code is often already self-explanatory — what it does is visible from the code itself. A comment is usually only helpful when it explains the reason behind a decision.

// Bad: just repeats the code
// prints Hello
System.out.println("Hello");

// Good: explains the reason
// Greeting for new users on the app's first launch
System.out.println("Hello");
Both comments sit above the same line of code — but only the second one provides information the code alone doesn't show.
True or false

The comment "// prints Hello" above System.out.println("Hello"); is an example of a good comment.

🤖
A comment that lies is worse than no comment at all. ⚠️

Rule 2: Keep comments up to date. When the code changes but the comment doesn't, you end up with an outdated comment. An outdated comment is often worse than no comment at all — it misleads others (and yourself).

// Prints the user's first name
System.out.println("Welcome back!");
This comment was probably not updated after the code changed — it no longer describes what the line actually does.
Fill in the blank

A comment that no longer matches the current code is called ____.

🤖
Comment overload can actually make code harder to read. 🌀

Rule 3: Don't over-comment. If every single, obvious line gets its own comment, the code becomes more cluttered instead of clearer. Use comments purposefully: to mark sections or to explain truly tricky parts.

// Bad: a comment for every single, obvious line
public class Main {
    public static void main(String[] args) {
        // prints text
        System.out.println("Hello");
    }
}

// Good: a comment marks only the important section
public class Main {
    public static void main(String[] args) {
        // --- Greeting ---
        System.out.println("Hello!");
        System.out.println("Glad you're here.");
    }
}
Comments should appear where they truly provide guidance — not on every simple line.
Quick check

When is a comment most useful?

Chapter quiz

Comments

10 questions · pass with 60% — just like in the app.

Quick check

What are comments in Java mainly used for?

True or false

The Java compiler executes the text in a comment like normal code.

Fill in the blank

A single-line comment in Java begins with the characters ____.

Quick check

What happens to the code after // on the same line?

True or false

A single-line comment with // automatically also applies to the next line.

Fill in the blank

A multi-line comment begins with /* and ends with ____.

Quick check

What is a multi-line comment (/* ... */) especially well-suited for?

True or false

With /* */, you can comment out multiple lines of code at once.

Quick check

What is an example of a good comment?

True or false

An outdated comment that no longer matches the code can do more harm than good.

Keep your streak going in Swipe Coding

Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →