Comments in Java - Single-line, Multi-line and Javadoc

⏱️ 5 min read • Beginner Level • Lesson 8

Lesson 8 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

Comments in Java are used to explain code, improve readability, and make programs easier to maintain. Comments are ignored by the Java compiler and do not affect program execution.


Types of Comments in Java

Java Supports Three Types of Comments
  • Single-line Comment
  • Multi-line Comment
  • Documentation (Javadoc) Comment

Single-line Comment

Single-line comments are used for short explanations and begin with //.

Demo.java
Copy Try Download
// This is a single-line comment
public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello World"); // prints output
    }
}
Explanation: Everything after // on a line is treated as a comment and ignored by the compiler.

Multi-line Comment

Multi-line comments are used for longer explanations and are written between /* and */.

Example.java
Copy Try Download
/*
 This is a multi-line comment
 It spans multiple lines
*/
public class Example {
    public static void main(String[] args) {
        System.out.println("Multi-line comment example");
    }
}
Explanation: Multi-line comments are useful when documenting larger blocks of code or temporarily disabling code during testing.

Documentation Comment (Javadoc)

Documentation comments are used to generate API documentation using the javadoc tool.

Demo.java
Copy Try Download
/**
 * This class demonstrates documentation comment.
 * @author ProwessApps
 * @version 1.0
 */
public class Demo {

    /**
     * Adds two numbers
     */
    public int add(int a, int b) {
        return a + b;
    }
}
Explanation: Javadoc comments begin with /** and are used to create professional API documentation.
Important Notes:
  • Comments improve code readability.
  • Comments do not affect program execution.
  • Single-line comments start with //.
  • Multi-line comments are enclosed within /* */.
  • Javadoc comments help generate documentation.
Summary:
  • Java supports 3 types of comments.
  • Single-line comments are used for short notes.
  • Multi-line comments are used for detailed explanations.
  • Documentation comments help generate API docs.

Interview Questions ⭐

Comments are non-executable lines in Java code that provide explanations or annotations for better readability.

Java supports three types of comments: single-line comments (//), multi-line comments (/* */), and Javadoc comments (/** */).

Next step: Learn Java Keywords

🚀 Continue to Java Keywords →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Comments | Language: Java

Question 1 of 5
Q1. Which symbol is used for single-line comments in Java?
Q2. Which tool processes Java documentation comments?
/** Example comment */
Q3. Which type of comment is best for multiple lines in Java?
Q4. Which of the following is a valid documentation comment in Java?
Q5. What happens if comments are placed inside code in Java?
System.out.println(10 /* comment */ + 20);

🎉 Great job! Keep learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.