Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Comments: Single-Line, Multi-Line, And Documentation Comments

Java Comments Tutorial

Comments are an essential part of any programming language, as they allow developers to write explanatory text alongside their code to provide context or guidance to other developers who may work on the same project. In Java, there are three types of comments: single-line, multi-line, and Javadoc comments.

  • Single-line Comments

Single-line comments are used to add brief explanations or to temporarily disable a specific line of code. They begin with two forward slashes (//), and anything following them on the same line will be treated as a comment.

Example:

// This is a single-line comment
int x = 5; // This comment is explaining the purpose of the variable x
  • Multi-line Comments

Multi-line comments, also known as block comments, span across multiple lines and are enclosed between a forward slash and an asterisk (/) at the beginning and an asterisk and a forward slash (/) at the end. They are useful for providing more detailed explanations or commenting out a block of code.

Example:

/*
 This is a multi-line comment.
 It can span across multiple lines.
*/
int y = 10;
  • Javadoc Comments

Javadoc comments are a special type of multi-line comment used to create documentation for Java classes, interfaces, methods, and fields. They begin with a forward slash and two asterisks (/**) and end with an asterisk and a forward slash (*/). Javadoc comments include specific tags that provide information about the documented code.

Example:

/**
 * This is a Javadoc comment for the ExampleClass.
 * It provides a brief description of the class's purpose.
 *
 * @author John Doe
 * @version 1.0
 */
public class ExampleClass {
    /**
     * This is a Javadoc comment for the add method.
     * It explains the purpose of the method and its parameters.
     *
     * @param a the first integer to add
     * @param b the second integer to add
     * @return the sum of a and b
     */
    public int add(int a, int b) {
        return a + b;
    }
}

In summary, comments are an important aspect of programming in Java, as they help improve the readability and maintainability of your code. Make sure to use comments effectively to explain the purpose and functionality of your code to other developers.

  1. Multi-line Comments in Java Examples: Multi-line comments are enclosed between /* and */ and can span multiple lines.

    /*
     * This is a multi-line comment
     * It spans multiple lines
     * Used for describing code sections
     */
    
  2. How to Comment Out Code in Java: To temporarily exclude code from execution, use single-line comments (//) or multi-line comments.

    // This line is a comment and won't be executed
    int x = 5;
    
    /*
     * The following code won't run either
     * int y = 10;
     */
    
  3. Javadoc Comments in Java and Their Significance: Javadoc comments are used for documentation and are enclosed between /** and */. They are processed by tools to generate documentation.

    /**
     * This method adds two numbers.
     * @param a The first number
     * @param b The second number
     * @return The sum of a and b
     */
    public int add(int a, int b) {
        return a + b;
    }
    
  4. Comment Conventions for Documenting Java Code: Follow conventions like using Javadoc comments for documenting classes and methods, and single-line or multi-line comments for code explanations.

    /**
     * Class description goes here.
     */
    public class MyClass {
        // Field description
        private int myField;
    
        /**
         * Method description.
         * @param param Description of the parameter
         */
        public void myMethod(int param) {
            // Code explanation
            int result = param + myField;
        }
    }
    
  5. Commenting for Code Readability in Java: Comments should be used judiciously to enhance code readability. They can explain complex logic, provide context, or clarify the purpose of code.

    // Check if the user is an admin
    if (userRole.equals("admin")) {
        // Perform admin-specific tasks
        performAdminTasks();
    }