In Java, comments are non-executable statements that provide explanations or annotations within the code. They are used to improve code readability and help developers understand the purpose and functionality of the code. Comments are ignored by the Java compiler during execution.
Types of Comments
Java supports three types of comments:
Single-line Comments
Multi-line Comments
Documentation Comments
Single-line Comments
Single-line comments start with // and continue until the end of the line. They are used for brief explanations or notes within the code.
Syntax
// This is a single-line comment
int number =10;// This initializes the variableĐược hỗ trợ bởi
Multi-line Comments
Multi-line comments, also known as block comments, begin with /* and end with */. They are useful for commenting out multiple lines of code or providing detailed explanations.
Syntax
/*
This is a multi-line comment.
It can span multiple lines.
*/
int number =20;Được hỗ trợ bởi
Documentation Comments
Documentation comments, or Javadoc comments, are special comments used to generate API documentation. They start with /** and end with */. These comments are typically placed before class, method, or field declarations to describe their purpose and usage.
Syntax
/**
* This is a documentation comment.
* @param args Command line arguments
*/publicstaticvoidmain(String[] args){// Code goes here}Được hỗ trợ bởi
Examples
Example 1: Using Single-line Comments
publicclassSingleLineCommentExample{publicstaticvoidmain(String[] args){// Print a message to the console
System.out.println("Hello, World!");// Output message}}Được hỗ trợ bởi
Example 2: Using Multi-line Comments
publicclassMultiLineCommentExample{publicstaticvoidmain(String[] args){/*
* This block of code prints a message
* to the console.
*/
System.out.println("Hello, Java!");}}Được hỗ trợ bởi
Example 3: Using Documentation Comments
/**
* The Calculator class provides basic arithmetic operations.
*/publicclassCalculator{/**
* Adds two integers.
* @param a First integer
* @param b Second integer
* @return Sum of a and b
*/public int add(int a, int b){return a + b;}}Được hỗ trợ bởi
Javadoc Tags
Javadoc comments can include specific tags to provide additional information about the code elements they describe. Commonly used tags include:
@param: Describes a method parameter.
@return: Describes the return value of a method.
@throws or @exception: Documents an exception thrown by a method.
Tips and Best Practices
Clarity: Use comments to clarify complex code logic, but avoid stating the obvious.
Consistency: Maintain a consistent style for comments throughout the codebase.
Update Comments: Ensure comments are updated when code changes to prevent discrepancies.
Avoid Over-commenting: Do not overuse comments; code should be self-explanatory where possible.
Javadoc: Use Javadoc comments for public APIs to facilitate automatic documentation generation.
In Java, comments are non-executable statements that provide explanations or annotations within the code. They are used to improve code readability and help developers understand the purpose and functionality of the code. Comments are ignored by the Java compiler during execution.
Types of Comments
Java supports three types of comments:
Single-line Comments
Single-line comments start with
//and continue until the end of the line. They are used for brief explanations or notes within the code.Syntax
Multi-line Comments
Multi-line comments, also known as block comments, begin with
/*and end with*/. They are useful for commenting out multiple lines of code or providing detailed explanations.Syntax
Documentation Comments
Documentation comments, or Javadoc comments, are special comments used to generate API documentation. They start with
/**and end with*/. These comments are typically placed before class, method, or field declarations to describe their purpose and usage.Syntax
Examples
Example 1: Using Single-line Comments
Example 2: Using Multi-line Comments
Example 3: Using Documentation Comments
Javadoc Tags
Javadoc comments can include specific tags to provide additional information about the code elements they describe. Commonly used tags include:
@param: Describes a method parameter.@return: Describes the return value of a method.@throwsor@exception: Documents an exception thrown by a method.Tips and Best Practices