Java Comments

Learn everything about Java comments including single line comments, multi line comments, documentation comments, syntax rules, examples, and best practices.

What are Comments in Java?

Comments are notes written inside a program to explain the code. They help developers understand the purpose of the program.

Java compiler ignores comments while executing the program.

Comments improve readability, maintainability, and understanding of code.

Types of Comments in Java

Java supports three types of comments:

Single Line Comment

Single line comments are used for short explanations. They start with double forward slashes //.

// This is single line comment

System.out.println("Java");

Output

Java

Everything written after // on the same line is ignored by Java compiler.

Single Line Comment Example

public class Main {

    public static void main(String[] args) {

        // Printing welcome message

        System.out.println("Welcome");

    }

}

Multi Line Comments

Multi line comments are used for large descriptions or explaining multiple lines of code.

They start with /* and end with */.

/*
This is multi line comment.
It can span multiple lines.
Java compiler ignores it.
*/

Example

public class Test {

    public static void main(String[] args) {

        /*
        This program prints
        Hello Java message
        */

        System.out.println("Hello Java");

    }

}

Documentation Comments

Documentation comments are used to generate official Java documentation using the Javadoc tool.

They start with /** and end with */.

/**
 * This is documentation comment
 * used for generating documentation.
 */

Example

/**
 * This class performs addition.
 */

public class Addition {

    /**
     * Main method
     */

    public static void main(String[] args) {

        System.out.println(10 + 20);

    }

}

Why are Comments Important?

Commenting Out Code

Developers often comment code temporarily for testing or debugging purposes.

public class Demo {

    public static void main(String[] args) {

        System.out.println("Line 1");

        // System.out.println("Line 2");

        System.out.println("Line 3");

    }

}

Output

Line 1
Line 3

Rules for Java Comments

Nested Comments are Invalid

Java does not support nested multi line comments.

/*
This is invalid

/*
Nested comment
*/

*/

Nested multi line comments cause compilation errors.

Best Practices for Writing Comments

Bad vs Good Comments

Bad Comment

// Increment i

i++;

This comment is unnecessary because the code is already obvious.

Good Comment

// Retry connection after network failure

retryConnection();

This comment explains the purpose of the code.

Complete Example

/**
 * Java Comments Example
 */

public class CommentsDemo {

    public static void main(String[] args) {

        // Variable declaration

        int age = 20;

        /*
        Printing age value
        on screen
        */

        System.out.println(age);

    }

}

Output

20

Common Mistakes

Conclusion

Comments are extremely useful in Java programming. They improve readability, teamwork, debugging, and maintainability of code.

Every developer should learn how to write proper comments to create clean and professional programs.