Java Comments
Comments help explain code and improve readability. Java supports both single and multi-line comments, as well as documentation (Javadoc) comments.
Single-Line Comments in Java
A single-line comment begins with two forward slashes: // and continues until the end of the line.
// this is a single-line comment
Output:
Hello, World!
Multi-Line Comments in Java
Multi-line comments start with a slash-asterisk /* and end with an asterisk-slash */. Everything inside a multi-line comment gets ignored.
/* multi-
line
comment
*/
Javadoc Comments
Javadoc is a tool used to automatically generate documentation, and it works by identifying comments that follow the style and conventions used for official Java documentation.
Javadoc comments use /** and */ to signify the start and end of a multiline comment, and each individual line starts with an asterisk *:
/** This is a Javadoc comment
* Each line of the Javadoc comment
* Starts with an asterisk
*/
Javadoc comments are typically written just before the declarations of methods, classes, and fields. Since Javadoc is used to automatically produce documentation, it’s important to take special care when writing these comments – as someone else may read them in the future.