Java Comments

Java Tutorial

Java Comments

Comments in Java are notes written inside the code to explain what the program or a particular line does. Comments make code more readable and help programmers understand logic easily — especially when coming back to code later.

 Comments are ignored by the Java compiler and do not affect the program output.


 Types of Comment in Java

Java supports three types of comment:

TypeFormatUsage
Single-line comment// commentShort explanations
Multi-line comment/* comment */Longer explanations or block comments
Documentation (JavaDoc) comment/** comment */Generate API documentation


 Single-Line Comment

Used to comment one line or short notes.

Example:


 Multi-Line Comment

Used when explanation spans multiple lines.


 Documentation Comment (JavaDoc)

Used to describe classes, methods, and generate HTML documentation using the javadoc tool.


 Why Use Comments?

  • To explain logic

  • To describe functions or variables

  • For debugging (temporarily disable code)

  • To help other developers understand code


 Commenting Out Code (Debugging)

You can disable code temporarily using comments:


 Wrong Usage Example

You cannot put a comment in the middle of a statement:


 Correct Usage


 Example Program With All Comments


 


 Summary

Comment TypeSymbolPurpose
Single-line//Small notes
Multi-line/* ... */Large explanation or block
JavaDoc/** ... */Generate documentation

You may also like...