Java Comments
How Comments Work
Comments are ignored and removed at compile time. After the comment definition, everything that follows will be ignored and removed by the compiler at compile time. Comments are usually placed on their own line to document the following line of code. Java supports three different forms of comments:
Single Line Comments
These are written starting with //.
// This is a single line comment.
They can also be added at the end of a line of code.
final var name = "test"; // test object name
Multi-line Comments
Mutli-line comments are comments that span one or more lines of code. They are written with /* */.
/*
This is
a multi-line
comment
*/
Javadoc
Javadoc is both a type of comment and a tool. The Javadoc tool is used to generate documentation from Javadoc comments. Javadocs are used to document a public API (Application Programming Interface). Javadocs use specific tags to create organized documentation for developers. HTML can also be used in Javadoc comments. Javadocs are a big subject, so only an example is shown here.
/**
* Reads and returns the contents of a file<br>
* Another line of the description...
*
* @param path The path to the file
* @param name The name of the file
* @return The contents of the file
* @throws IOException If the file is not found.
*/
public String read(
final String path,
final String name) throws IOException {
// ...
}
Conclusion
Java supports three different types of comments. Use a single or multi-line comment to document lines of code in your application. Use a Javadoc for documenting the public API.