Back to Writing Classes

Lesson PreviewLesson 3 of 10 in this unit

5.3 Documentation with Comments

Part of Writing Classes in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

In programming, comments are lines of text in code meant for humans to read. The computer ignores these lines when running the program. Comments help explain what your code does, making it easier to understand and maintain.

java
// This is a single-line comment
// Anything after the double slashes is ignored by the computer

/*
This is a multi-line comment.
You can use it for longer explanations or to temporarily disable code.
*/

// Example of both types of comments in action
public class Main {
    public static void main(String[] args) {
        // Print a message to the console
        System.out.println("Hello, World!"); // This prints text
    }
}

Output

Hello, World!

In this example, the comments are ignored by the compiler, and only the System.out.println statements are executed.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.