Java Comments
Java Comments
Java Comments
Java comments are text notes written in the code to provide an explanation about the source
code. The comments can be used to explain the logic or for documentation purposes. The
compiler does not compile the comments. In Java, comments are very similar to C and C++.
Single-line comments
Multiline comments
Documentation comments
The single line comments are the most used commenting way to explain the purpose (or to add a
text note) of the line.
Syntax
// comment
Following code shows the usage of single line comments in a simple program. We've added
comments to code lines to explain their purpose.
package com.tutorialspoint;
Output
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0
2. Multiline Comment
The multiline (or, multiple-line) comments start with a forward slash followed by an asterisk (/*)
and end with an asterisk followed by a forward slash (*/) and they are used to add comment on
multiple lines.
The multiline comments are very useful when we want to put a long comment spreading across
multiple lines or to comment out the complete code.
Syntax:
/* This is an example
of
multi line comment. */
/* if (dividend == 0) {
throw new IllegalArgumentException("dividend cannot be zero");
} */
Following code shows the usage of multiple comments in a simple program. We've commented
out extra code from a method using Multiline comments.
package com.tutorialspoint;
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0
3. Documentation Comment
The documentation comments are used for writing the documentation of the source code. The
documentation comments start with a forward slash followed by the two asterisks (/**), end
with an asterisk followed by a backward slash (*/), and all lines between the start and end must
start with an asterisk (*).
The documentation comments are understood by the Javadoc tool and can be used to create
HTML-based documentation.
Syntax
/**
* line 1
* line 2
...
*/
/**
* This is a documentation comment.
* This is my first Java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/
public class MyFirstJavaProgram {}
The above commenting style is known as documentation comments. It is used by Javadoc tool
while creating the documentation for the program code. We can give details of arguments,
exception and return type as well using following annotation in documentation comments.
/**
* @param dividend
* @param divisor
* @return quotient
* @throws IllegalArgumentException if divisor is zero
*/
private double divide(int dividend, int divisor) throws IllegalArgument
}
Following code shows the usage of documentation comments in a simple program. We've
defined a comments on the class declaration to give details of the class. In case of method, we're
adding details of parameters, return value and exception raised in documentation block of the
method comments section.
package com.tutorialspoint;
/**
* This is a documentation comment.
* This is my first Java program.
* This is an example of multi-line comments.
* We're printing result of divison of two numbers in this program
*/
public class MyFirstJavaProgram {
public static void main(String[] args) {
MyFirstJavaProgram program = new MyFirstJavaProgram();
double result = program.divide(100, 10);
System.out.println(result);
}
/**
* @param dividend
* @param divisor
* @return quotient
* @throws IllegalArgumentException if divisor is zero
*/
private double divide(int dividend, int divisor) throws IllegalArgum
if (divisor == 0) {
throw new IllegalArgumentException("divisor cannot be zero");
}
return (double) dividend / divisor;
}
}
Output
Compile and run MyFirstJavaProgram. This will produce the following result −
10.0