Skip to main content
Documents
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Java Comments

In Java, comments are non-executable statements that provide explanations or annotations within the code. They are used to improve code readability and help developers understand the purpose and functionality of the code. Comments are ignored by the Java compiler during execution.

Types of Comments

Java supports three types of comments:

  1. Single-line Comments
  2. Multi-line Comments
  3. Documentation Comments

Single-line Comments

Single-line comments start with // and continue until the end of the line. They are used for brief explanations or notes within the code.

Syntax

 // This is a single-line comment
int number = 10; // This initializes the variable

Multi-line Comments

Multi-line comments, also known as block comments, begin with /* and end with */. They are useful for commenting out multiple lines of code or providing detailed explanations.

Syntax

 /*
This is a multi-line comment.
It can span multiple lines.
*/
int number = 20;

Documentation Comments

Documentation comments, or Javadoc comments, are special comments used to generate API documentation. They start with /** and end with */. These comments are typically placed before class, method, or field declarations to describe their purpose and usage.

Syntax

 /**
 * This is a documentation comment.
 * @param args Command line arguments
 */
public static void main(String[] args) {
    // Code goes here
}

Examples

Example 1: Using Single-line Comments

 public class SingleLineCommentExample {
    public static void main(String[] args) {
        // Print a message to the console
        System.out.println("Hello, World!"); // Output message
    }
}

Example 2: Using Multi-line Comments

 public class MultiLineCommentExample {
    public static void main(String[] args) {
        /*
         * This block of code prints a message
         * to the console.
         */
        System.out.println("Hello, Java!");
    }
}

Example 3: Using Documentation Comments

 /**
 * The Calculator class provides basic arithmetic operations.
 */
public class Calculator {
    /**
     * Adds two integers.
     * @param a First integer
     * @param b Second integer
     * @return Sum of a and b
     */
    public int add(int a, int b) {
        return a + b;
    }
}

Javadoc Tags

Javadoc comments can include specific tags to provide additional information about the code elements they describe. Commonly used tags include:

  • @param: Describes a method parameter.
  • @return: Describes the return value of a method.
  • @throws or @exception: Documents an exception thrown by a method.

Tips and Best Practices

  • Clarity: Use comments to clarify complex code logic, but avoid stating the obvious.
  • Consistency: Maintain a consistent style for comments throughout the codebase.
  • Update Comments: Ensure comments are updated when code changes to prevent discrepancies.
  • Avoid Over-commenting: Do not overuse comments; code should be self-explanatory where possible.
  • Javadoc: Use Javadoc comments for public APIs to facilitate automatic documentation generation.

Learn Java Essentials

Build your Java skills from the ground up and master programming concepts.
Start Learning Java for Free