Comment Syntax in Java

In Java, comments are used to provide explanatory notes or remarks within the code that are ignored by the compiler during the compilation process. Comments are helpful for code documentation and making it more understandable for developers. In this blog post, we will learn comment syntax in Java with example.

Types of comment in Java

Java supports three types of comments:

  • Single Line
  • Multi Line
  • Documentation

Single-line comments

Single-line comments begin with // and continue until the end of the line. Anything written after // on the same line is considered a comment and is ignored by the compiler.


// This is a single-line comment in Java
int x = 10; // This is another comment after a statement

Multi-line comments (also known as block comments)

Multi-line comments start with /* and end with */. Everything written between these delimiters is considered a comment and is ignored by the compiler. This type of comment can span across multiple lines.


  This is a multi-line comment in Java.
  It can span multiple lines.
  Useful for longer explanations or disabling blocks of code.
int x = 10;
int y = 20;

Documentation Comments

JavaDoc (documentation) comments are used to generate API documentation automatically. They start with /** and end with */. These comments are often placed directly before classes, methods, or fields to describe their purpose and usage. JavaDoc comments follow a specific syntax and can include special tags like @param, @return, and @throws to provide additional information about the elements being documented.


 * This method calculates the sum of two integers.
 * @param a The first integer.
 * @param b The second integer.
 * @return The sum of the two integers.
public int calculateSum(int a, int b) {
    return a + b;

JavaDoc comments are particularly helpful in creating well-documented APIs and are widely used in Java libraries and projects to provide clear and structured documentation.

Frequently Asked Questions

What is the purpose of comments in Java?

Comments in Java are used to provide explanations and documentation within the code. They are ignored by the compiler and are meant for developers to understand the code.

What are the two types of comments in Java, and how do they differ?

Java supports two types of comments: single-line comments (//) and multi-line comments (/* */). Single-line comments are used for brief explanations, while multi-line comments are suitable for longer descriptions.

Can I nest multi-line comments in Java?

No, Java does not support nesting of multi-line comments. Attempting to nest them will result in a compilation error.

Is it a good practice to comment every line of code?

While comments are helpful for clarity, excessive comments can clutter the code. It’s generally best to use comments judiciously to explain complex logic or provide context.

Do comments affect the performance or size of the compiled Java code?

No, comments have no impact on the performance or size of the compiled code. They are entirely removed during the compilation process and do not affect the program’s execution.