Overview
Comments are notes in the source code that explain what the code does. The Java compiler ignores them,
so they never affect how the program runs. Java has three types of comments, and this
program uses all of them.
The comment type depends on the syntax, not the number of lines. /* ... */ is a multi-line comment even when it fits on a single line.
Step-by-Step Explanation
1
Documentation Comment
Starts with /** and is read by the javadoc tool.
/**
* Documentation comment (Javadoc).
* This program demonstrates the three types of comments in Java.
*
* @author Rumman Ansari
*/
Documentation comments are placed above classes and methods. The javadoc tool reads them to create HTML documentation.
Tags such as @author add structured information.
2
Multi-Line Comment
Starts with /* and ends with */.
/* This is a multi-line comment.
* It can span several lines.
* The compiler ignores everything between the opening and closing markers.
*/
Everything between /* and */ is ignored, however many lines it covers.
The * at the start of each middle line is optional and only there for readability.
3
Single-Line Comment
Starts with // and ends at the end of the line.
// This is a single-line comment
Use this for short notes. Anything after // on that line is ignored.
4
Multi-Line Comment on One Line
Still a multi-line comment.
/* This is a multi-line comment written on one line */
This uses the /* */ syntax, so it's a multi-line comment, even though it takes only one line.
5
Print Statement
Proves the comments don't affect output.
System.out.println("This program is an example of comments in Java.");
Only this statement and the next ones run. None of the comments above it appear in the output.
6
Comment After Code
Code runs, the comment is ignored.
int marks = 85; // A single-line comment can also appear after code
System.out.println("Marks: " + marks);
The statement before // runs normally, and the text after it is ignored. The program then prints Marks: 85.
Types of Comments in Java
| Type |
Syntax |
Best Used For |
| Single-line |
// comment |
Short notes, or switching off one line while testing |
| Multi-line |
/* comment */ |
Longer explanations, or switching off a block of code |
| Documentation |
/** comment */ |
Describing classes and methods for the javadoc tool |
Output
This program is an example of comments in Java.
Marks: 85
Common Mistakes
Nesting multi-line comments
/* outer /* inner */ still outer */ doesn't work. The first */ ends the comment, and the remaining text causes a compile error.
Forgetting the closing */
If you leave out */, the rest of the file becomes a comment and you get an unclosed comment error.
Calling /* */ a single-line comment
The comment type depends on the syntax. /* */ is always a multi-line comment.
Good practice
Use /** */ above public classes and methods, // for short notes inside methods, and keep comments up to date when you change the code.
Generate Documentation (Optional)
javadoc -d docs MultiLineCommentExample.java
This creates a docs folder with HTML documentation built from the /** */ comments. Open docs/index.html in a browser to view it.
Key Takeaway
Java has three comment types: // for single-line notes, /* */ for multi-line notes
and /** */ for documentation. The compiler ignores all of them, so they explain code without changing the output.