Comments are a very important part of Java code. They are like “instruction manuals” for your code, making it clearer and easier to understand for yourself and others. Comments help in quickly locating issues during debugging. Compilers automatically ignore comment content, so they do not affect the execution of the code. Let’s now explore the three types of comments in Java in detail: single-line comments, multi-line comments, and documentation comments.
1. Single-Line Comments (//)¶
Format: Start with //, followed by the comment content. It can only comment on a single line.
Example:
// This is a single-line comment. I can write it after a code line.
int score = 95; // Define the student's score as 95.
// I can also write a single-line comment on a separate line to explain the purpose of a code segment.
// For example, the following line calculates the sum of two numbers.
int sum = 10 + 20;
Notes:
- Single-line comments can only comment on a single line. To comment on multiple lines, you need to use // for each line.
- Single-line comments cannot be nested. For example, // This is a // nested comment will be considered a syntax error by the compiler.
2. Multi-Line Comments (/* */)¶
Format: Start with /* and end with */. The content between them can span multiple lines.
Example:
/*
This is a multi-line comment that can span multiple lines.
I can write detailed explanations about a code segment here.
For example, the purpose of the following method is to print user information.
*/
public void printUserInfo(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
/*
Note: Multi-line comments cannot be nested!
For example, the following syntax will cause an error:
/* This is a nested comment */
// The correct approach is to treat the nested part as regular code or split it into multiple single-line comments.
Notes:
- Multi-line comments can span multiple lines but cannot be nested (i.e., you cannot write /* */ inside another /* */).
- Multi-line comments are typically used to explain the overall logic of a code segment, such as the purpose of a class or the steps of a complex algorithm.
3. Documentation Comments (/** */)¶
Format: Start with /** and end with */. These are specifically used to generate API documentation (e.g., using the Javadoc tool to create help documents).
Features: They can include tags (e.g., @author, @param, @return), but for beginners, mastering the basic format is sufficient first.
Example:
/**
* This is a documentation comment used to generate help documents.
* @author Xiaoming (Author information)
* @version 1.0 (Version number)
*
* This class is used to manage user information.
*/
public class User {
private String name;
private int age;
/**
* Constructor to initialize user information.
* @param name User's name
* @param age User's age
*/
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
Purpose: Using the Javadoc tool (a built-in documentation generation tool in Java), documentation comments can be automatically converted into HTML-formatted API documentation, making it easy for others to view descriptions of classes, methods, and fields.
Commenting Guidelines and Best Practices¶
- Avoid Redundant Comments: For example,
// Define a variableis meaningless. Writing code directly is clearer. - Highlight Key Points: Comments should explain “why” and “the purpose” of the code, not just “what” the code does.
- Keep Comments Updated: If the code logic changes, update the comments to avoid misleading others.
- Use Comments Appropriately:
- Classes/Interfaces: Use documentation comments (/** */) to describe their purpose.
- Methods: Use documentation comments to explain parameters, return values, and exceptions.
- Complex Logic: Use multi-line comments (/* */) to break down steps.
- Variables/Code Lines: Use single-line comments (//) for brief supplementary explanations.
Summary¶
- Single-line Comments: Quickly comment on single-line content, suitable for brief explanations.
- Multi-line Comments: Explain the overall logic of a code segment, suitable for complex logic.
- Documentation Comments: Generate API documentation, suitable for standardized descriptions of classes and methods.
Using comments reasonably makes your code “speak for itself,” facilitating future maintenance and smoother team collaboration. Remember: Good comments are a plus for code quality, not a burden!