




- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


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 through how clearly it’s explained.
Comments don’t affect how a program runs. Instead, they help other people (and your future self) understand, maintain, and debug the code more easily.
You can use comments to:
- Clarify complex logic
- Mark sections for future improvement
- Disable parts of the code temporarily
- Generate documentation (JavaDoc)
Types of Comments in Java
Java supports three main types of comments:
- Single-line Comments
- Multi-line Comments
- Documentation 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
Multi-line Comments are used when you want to explain something in more than one line. These can also be used to temporarily disable 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
- Write comments that add value—don't repeat the code.
- Keep them up-to-date. Outdated comments can mislead developers.
- Use comments to explain "why" more than "what".
- Avoid cluttering your code with excessive comments—clarity is key.
Common Mistakes to Avoid while Writing Comments
- Using comments to excuse bad code. Refactor instead.
- Leaving debug comments in production code.
- Overusing JavaDoc for private/internal methods that don’t need documentation.
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");
// System.out.println("Commented out");
System.out.println("Active line");