Multi Line Comments in Java
2 min read ·
Multi line comments in Java are used to write explanations or notes that span across multiple lines. Like single line comments, they are ignored by the Java compiler and do not affect program execution.
Using Multi Line Comments
A multi line comment starts with
/* and ends with */.Everything written between
/* and */ is treated as a comment.Explaining Code Blocks
Multi line comments are useful for explaining larger sections of code.
They are often placed above methods or logic blocks.
Commenting Multiple Lines of Code
You can comment out multiple lines of code at once using multi line comments.
Only the active code is executed.
Note
Multi line comments are helpful when testing or debugging code.
Common Mistakes with Multi Line Comments
Forgetting to close the comment
Nesting multi line comments
Placing comments inside strings
The above code will cause an error because Java does not support nested multi line comments.
Caution
Always make sure every multi line comment has a proper ending.
Difference from Single Line Comments
Multi line comments are better for:
Long explanations
File headers
Temporary code blocks
Single line comments are better for:
Short explanations
Inline notes
Why Multi Line Comments Are Important
Multi line comments help you:
Document code clearly
Explain complex logic
Improve long term maintainability
Goal Achieved
You now understand how to use multi line comments correctly in Java.