Overview
A multi-line comment (also called a block comment) starts with /* and ends with */.
The compiler ignores everything between these two markers, however many lines it covers.
It's useful for longer explanations that don't fit comfortably on one line.
A multi-line comment ends at the first */ the compiler finds. That's why you can't put one multi-line comment inside another.
Step-by-Step Explanation
1
Plain Multi-Line Comment
Text spread across several lines.
/* This is a multi-line comment.
In this program, we are going to
declare a variable and print its value in Java. */
The comment opens with /* on the first line and closes with */ on the last line. All three lines are ignored.
2
Declare and Print a Variable
The code that actually runs.
int number = 10;
System.out.println(number);
This creates an integer variable number with the value 10 and prints it.
3
Star-Prefixed Style
The most common professional style.
/*
* A multi-line comment can also be written
* with a * at the start of each line.
* This style is easier to read.
*/
The * at the start of each middle line is optional. Java treats it as part of the comment text, but it makes the comment block easier to see.
4
Inline Multi-Line Comment
A comment inside a statement.
int total = number /* inline comment */ + 5;
System.out.println(total);
Because /* */ has a clear end marker, it can sit in the middle of a line. The compiler reads this as number + 5, so 15 is printed.
A // comment can't do this, because it hides everything to the end of the line.
Single-Line vs Multi-Line Comment
| Feature |
Single-Line // |
Multi-Line /* */ |
| Ends at |
End of the line |
The closing */ |
| Covers several lines |
No, needs // on each line |
Yes |
| Can sit inside a statement |
No |
Yes |
| Best for |
Short notes |
Longer explanations, or switching off a block of code |
Output
10
15
Common Mistakes
Nesting comments
In /* outer /* inner */ outer */, the first */ ends the comment. The leftover outer */ causes a compile error.
Forgetting the closing */
Without */, the rest of the file becomes a comment and you get an unclosed comment error.
Adding a space inside the marker
/ * or * / isn't a comment marker. There must be no space between / and *.
Good practice
Wrap a block of code in /* */ to switch it off while testing. Make sure that block has no */ inside it.
Key Takeaway
A multi-line comment starts with /* and ends with */. It can cover many lines or sit inside a statement,
but it can't be nested. The compiler ignores it, so the output stays the same.