Comment Syntax in Java

Java is a popular programming language used in a variety of applications, including web development, mobile app development, and more. One important aspect of programming in any language is the ability to add comments to our code.

In Java, there are three types of comments:

  • Single-line comments
  • Multi-line comments and
  • Documentation comment

Single-Line Comment

Single-line comments are used to add a brief note or explanation to a line of code. They begin with two forward slashes (//) and continue until the end of the line. For example:

int x = 10; // This line declares a variable x and assigns it the value of 10

Multi-Line Comment

Multi-line comments are used to add a longer explanation or block of notes to our code. They begin with a forward slash and a single asterisk (/*) and end with an asterisk and a forward slash (*/). Anything between these symbols will be ignored by the compiler. For example:

This is a multi-line comment.
It can span multiple lines and is often used to provide
a detailed explanation of complex code.

It’s important to note that Java comments are not executed by the compiler, meaning they have no effect on the behavior of your program. They are simply there to help us who writes the code, or other developers reading our code, understand what is happening in the program.

Using comments effectively can greatly improve the readability and maintainability of our code. It’s a good practice to add comments to our code, especially in complex or unfamiliar sections, to make it easier for ourselves or others to understand what is happening. Just be sure to keep our comments concise and relevant to the code they are explaining.

Documentation comment

In addition to single-line and multi-line comments, Java also supports a type of comment known as a documentation comment. Documentation comments are used to generate documentation for our code in the form of HTML pages. They are similar to multi-line comments, but begin with a forward slash and double asterisks (/**) and end with a single asterisk and a forward slash (*/).

Documentation comments are often used to provide detailed explanations of the purpose and use of classes, methods, and other code elements. They can include special tags, such as @param and @return, which provide additional information about the code being documented. This information can then be extracted and used to generate API documentation for our code.

To use documentation comments, we have to use a documentation generation tool, such as javadoc. This tool will parse our code and extract the documentation comments, generating HTML pages that contain the documentation for our code. Documentation comments are a useful tool for creating clear and comprehensive documentation for our code and can help make it easier for others to understand and use our code.

Here is an example of a documentation comment for a Java method:

 * This is a documentation comment for the addNumbers method.
 * This method takes two integer arguments and returns the sum of the two numbers.
 * @param a The first number to be added.
 * @param b The second number to be added.
 * @return The sum of the two numbers.
 public int addNumbers(int a, int b) {
    return a + b;

In this example, the documentation comment provides a brief description of the purpose and behavior of the addNumbers method. It also includes special tags, such as @param and @return, which provides additional information about the method’s arguments and return value.

When this code is processed by a documentation generation tool, such as javadoc, the documentation comment will be extracted and included in the generated documentation for the method. This can make it easier for other developers to understand and use the method in their own code.


In Java, comments are used to add notes and explanations to code. They come in three forms: single-line comments, multi-line comments, and documentation comments. Documentation Comments, which begin with /** and end with */, can also be used to generate HTML documentation. Using comments effectively can improve the readability and maintainability of code.