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++.
In Java, there are three types of comments:
- Single-line comments
- Multiline comments
- Documentation comments
Let’s discuss each type of comment in detail.
1. Single Line Comment
The single-line comment is used to add a comment on only one line and can be written by using the two forward slashes (//). These comments are the most used commenting way.
The single line comments are the most used commenting way to explain the purpose (or to add a text note) of the line.
Syntax
Consider the below syntax to write a single line comment in Java:
// comment
Example 1: Java Single Line Comment
// if divisor is 0 throw an exception
if (divisor == 0) {
throw new IllegalArgumentException("divisor cannot be zero");
}
Example 2: Java Single Line 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.
public class MyFirstJavaProgram {
public static void main(String[] args) {
MyFirstJavaProgram program = new MyFirstJavaProgram();
double result = program.divide(100, 10);
System.out.println(result);
}
private double divide(int dividend, int divisor) throws IllegalArgumentException {
// if divisor is 0 throw an exception
if (divisor == 0) {
throw new IllegalArgumentException("divisor cannot be zero");
}
return (double) dividend / divisor; // returns the result of the division as double
}
}
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:
Consider the below syntax to write multiline comment in Java:
/*
Comment (line 1)
Comment (line 2)
...
*/
Example 1: Java Multiline Comment
/* This is an example
of
multi line comment. */
/* if (dividend == 0) {
throw new IllegalArgumentException("dividend cannot be zero");
} */
Example 2: Java Multiline Comment
Following code shows the usage of multiple comments in a simple program. We’ve commented out extra code from a method using Multiline comments.
public class MyFirstJavaProgram {
public static void main(String[] args) {
MyFirstJavaProgram program = new MyFirstJavaProgram();
double result = program.divide(100, 10);
System.out.println(result);
}
private double divide(int dividend, int divisor) throws IllegalArgumentException {
if (divisor == 0) {
throw new IllegalArgumentException("divisor cannot be zero");
}
/* if (dividend == 0) {
throw new IllegalArgumentException("dividend cannot be zero");
} */
return (double) dividend / divisor;
}
}
Output
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
Consider the below syntax to write documentation comment in Java:
/**
* line 1
* line 2
...
*/
Example 1: Java Documentation Comment
/**
* 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 IllegalArgumentException {
}
Example 2: Java Documentation Comment
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.
/**
* 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 IllegalArgumentException {
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