Topic 5 – Comments in Java Programming
Java Comments
The Java comments are the statements in a program that are not executed by the compiler and interpreter.
Why we use comments in a code
o Comments are used to make the program more readable by adding the details of the code.
o It makes easy to maintain the code and to find the errors easily.
o The comments can be used to provide information or explanation about the variable, method, class, or
any statement.
o It can also be used to prevent the execution of program code while testing the alternative code.
Types of Java Comments
There are three types of comments in Java.
1. Single Line Comment
2. Multi Line Comment
3. Documentation Comment
1) Java Single Line Comment
The single-line comment is used to comment only one line of the code. It is the widely used and easiest way of
commenting the statements.
Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java.
Syntax:
1. //This is single line comment
2. Sample Code
[Link]
1. public class CommentExample1 {
2. public static void main(String[] args) {
3. int i=10; // i is a variable with value 10
4. [Link](i); //printing the variable i
5. }
6. }
Output:
10
2) Java Multi Line Comment
The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code
snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line comments
there).
Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java.
Syntax:
1. /*
2. This
3. is
4. multi line
5. comment
6. */
Sample Code
[Link]
1. public class CommentExample2 {
2. public static void main(String[] args) {
3. /* Let's declare and
4. print variable in java. */
5. int i=10;
6. [Link](i);
7. /* float j = 5.9;
8. float k = 4.4;
9. [Link]( j + k ); */
10. }
11. }
Output:
10
Note: Usually // is used for short comments and /* */ is used for longer comments.
3) Java Documentation Comment
Documentation comments are usually used to write large programs for a project or software application as it
helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods,
arguments, etc., are used in the code.
To create documentation API, we need to use the javadoc tool. The documentation comments are placed
between /** and */.
Syntax:
1. /**
2. *
3. *We can use various tags to depict the parameter
4. *or heading or author name
5. *We can also use HTML tags
6. *
7. */
javadoc tags
Some of the commonly used tags in documentation comments:
Tag Syntax Description
{@docRoot} {@docRoot} to depict relative path to root directory of generated
document from any page.
@author @author name - text To add the author of the class.
@code {@code text} To show the text in code font without interpreting it as
html markup or nested javadoc tag.
@version @version version-text To specify "Version" subheading and version-text when -
version option is used.
@since @since release To add "Since" heading with since text to generated
documentation.
@param @param parameter-name To add a parameter with given name and description to
description 'Parameters' section.
@return @return description Required for every method that returns something
(except void)
Example of Javadoc tag in a Java program.
[Link]
1. import [Link].*;
2.
3. /**
4. * <h2> Calculation of numbers </h2>
5. * This program implements an application
6. * to perform operation such as addition of numbers
7. * and print the result
8. * <p>
9. * <b>Note:</b> Comments make the code readable and
10. * easy to understand.
11. *
12. * @author Anurati
13. * @version 16.0
14. * @since 2021-07-06
15. */
16.
17. public class Calculate{
18. /**
19. * This method calculates the summation of two integers.
20. * @param input1 This is the first parameter to sum() method
21. * @param input2 This is the second parameter to the sum() method.
22. * @return int This returns the addition of input1 and input2
23. */
24. public int sum(int input1, int input2){
25. return input1 + input2;
26. }
27. /**
28. * This is the main method uses of sum() method.
29. * @param args Unused
30. * @see IOException
31. */
32. public static void main(String[] args) {
33. Calculate obj = new Calculate();
34. int result = [Link](40, 20);
35.
36. [Link]("Addition of numbers: " + result);
37. }
38. }
Compile it by javac tool:
Create Document
Create documentation API by javadoc tool:
Now, the HTML files are created for the Calculate class in the current directory, i.e., abcDemo. Open the HTML
files, and we can see the explanation of Calculate class provided through the documentation comment.
Are Java comments executable?
Ans: As we know, Java comments are not executed by the compiler or interpreter, however, before the lexical
transformation of code in compiler, contents of the code are encoded into ASCII in order to make the
processing easy.
[Link]
1. public class Test{
2. public static void main(String[] args) {
3. //the below comment will be executed
4. // \u000d [Link]("Java comment is executed!!");
5. }
6. }
Output:
The above code generates the output because the compiler parses the Unicode character \u000d as a new
line before the lexical transformation, and thus the code is transformed as shown below:
[Link]
1. public class Test{
2. public static void main(String[] args) {
3. //the below comment will be executed
4. //
5. [Link]("Java comment is executed!!");
6. }
7. }
Thus, the Unicode character shifts the print statement to next line and it is executed as a normal Java code.