Java Comments: Enhancing Code Readability and Maintenance

Discover the importance of comments in Java, which are ignored by the compiler and interpreter but play a crucial role in making code more readable and maintainable. This guide covers the three types of Java comments: Single Line Comments, Multi Line Comments, and Documentation Comments. Learn how to effectively use comments to provide explanations and improve the overall quality of your Java code.



Java Comments

Comments in Java are not executed by the compiler and interpreter. They are used to make the code more readable, help maintain the code, and provide explanations about the code.

Types of Java Comments

  • Single Line Comment
  • Multi Line Comment
  • Documentation Comment
Single Line Comment

Single line comments start with //. Any text after // is not executed by Java.

Example

public class CommentExample1 {
public static void main(String[] args) {
int i = 10; // i is a variable with value 10
System.out.println(i); // printing the variable i
}
}
Output

10
Multi Line Comment

Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java.

Example

public class CommentExample2 {
public static void main(String[] args) {
/* This is a multi-line comment
that spans multiple lines */
int i = 10;
System.out.println(i);
}
}
Output

10
Documentation Comment

Documentation comments are used to create documentation API and are placed between /** and */. The javadoc tool is used to generate the API documentation.

Example

import java.io.*;

/**
* 

Calculation of numbers

* This program adds numbers and prints the result. * * @author Anurati * @version 16.0 * @since 2021-07-06 */ public class Calculate { /** * This method calculates the summation of two integers. * @param input1 This is the first parameter to sum() method. * @param input2 This is the second parameter to sum() method. * @return int This returns the addition of input1 and input2. */ public int sum(int input1, int input2) { return input1 + input2; } public static void main(String[] args) { Calculate obj = new Calculate(); int result = obj.sum(40, 20); System.out.println("Addition of numbers: " + result); } }
Output

Addition of numbers: 60
Javadoc Tags

Some commonly used tags in documentation comments:

Tag Syntax Description
{@docRoot} {@docRoot} Relative path to root directory of generated document from any page.
@author @author name - text Add the author of the class.
@code {@code text} Show the text in code font without interpreting it as HTML markup or nested javadoc tag.
@version @version version-text Specify "Version" subheading and version-text when -version option is used.
@since @since release Add "Since" heading with since text to generated documentation.
@param @param parameter-name description Add a parameter with given name and description to 'Parameters' section.
@return @return description Required for every method that returns something (except void).

Note: Java comments are not executed by the compiler or interpreter, but they can be processed if encoded in Unicode.

Example

public class Test {
public static void main(String[] args) {
// The below comment will be executed
// \u000d System.out.println("Java comment is executed!!");
}
}
Output

Java comment is executed!!