Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Comments
Single-line, Multi-line, and Documentation Comments



Comments in Java

Every great codebase tells a story—not just through what it does, but how clearly it's explained. Comments in Java are a crucial part of that narrative. They don't affect the program execution, but they enrich the code for the humans who read it, maintain it, and debug it.

Whether you're working on your own project or collaborating with a team, using comments properly ensures that your intentions are clearly communicated.

Types of Comments in Java

Java supports three main types of comments:

1. Single-line Comments

These are used for short explanations or notes.

// This is a single-line comment
System.out.println("Hello, World!"); // Print statement
Hello, World!

Explanation: The lines starting with // are ignored by the compiler. Only the System.out.println() line executes and prints the output.

2. Multi-line Comments

Used when you want to explain something in more than one line. Helpful for temporarily disabling blocks of code during testing or debugging.

/* This is a multi-line comment.
   It can span several lines.
   Useful for detailed notes or explanations. */
System.out.println("Multi-line comment demo");
Multi-line comment demo

Note: Everything between /* and */ is ignored by the compiler.

3. Documentation Comments (JavaDoc)

Specially formatted comments used to generate official documentation. These comments are placed just before class, method, or field declarations.

/**
 * This class demonstrates JavaDoc comments.
 * @author Mallikarjuna
 * @version 1.0
 */
public class Example {

    /**
     * This method prints a greeting.
     * @param name The name to greet
     */
    public void greet(String name) {
        System.out.println("Hello, " + name);
    }
}

Usage Tip: Use the javadoc tool to generate HTML documentation from JavaDoc comments:

javadoc Example.java

Best Practices for Commenting in Java

Common Mistakes to Avoid

How to Verify if Comments Are Working

Actually, you verify by what doesn’t happen:

Conclusion

Mastering comments is not just about syntax; it’s about thinking like a communicator. Clean, concise, and purposeful comments set apart a novice coder from a thoughtful developer. Use them wisely to make your code speak not just to the machine, but to your future self and fellow programmers.

QUIZ

Question 1:Which of the following is used for a single-line comment in Java?

Question 2:Multi-line comments in Java can be used to temporarily disable blocks of code.

Question 3:What is the correct way to write a documentation comment for a method in Java?

Question 4:Which of the following are considered good practices for writing comments in Java?

Question 5:JavaDoc comments are required for all private methods in a class.

Question 6:What happens when you execute the following code?
// System.out.println("Commented out");
System.out.println("Active line");



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M