Home› Java Programming Language› / Programs / Java Program to Demonstrate Single-Line, Multi-Line and Documentation Comments
🚀 Programming Example

Java Program to Demonstrate Single-Line, Multi-Line and Documentation Comments

5,688 Views
Java Java 8 or later
Beginner
Demonstration

Learn the three types of comments in Java: single-line (//), multi-line (/* */) and documentation (/** */) comments. This program uses each type and shows that the compiler ignores comments, so they never change the output.

Before You Begin

What you will learn

  • Write single-line comments using //
  • Write multi-line comments using /* */
  • Write documentation comments using /** */ with tags such as @author
  • Understand that /* */ on one line is still a multi-line comment
  • Know when to use each type of comment
  • Understand that the compiler ignores comments, so they don't affect the output

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • Basic idea of variables and the int data type
  • How to compile with javac and run with java

📌 Information & Algorithm

Algorithm

  1. Start
  2. Add a documentation comment above the class to describe the program.
  3. Create a public class named MultiLineCommentExample.
  4. Add a multi-line comment inside the class.
  5. Define the main() method.
  6. Add a single-line comment and a one-line multi-line comment.
  7. Display "This program is an example of comments in Java."
  8. Declare an integer variable marks, assign it 85, and add a comment at the end of the line.
  9. Display the value of marks.
  10. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac MultiLineCommentExample.java
java MultiLineCommentExample

💻 Program Code

/**
 * Documentation comment (Javadoc).
 * This program demonstrates the three types of comments in Java.
 *
 * @author Rumman Ansari
 */
public class MultiLineCommentExample {

    /* This is a multi-line comment.
     * It can span several lines.
     * The compiler ignores everything between the opening and closing markers.
     */

    public static void main(String[] args) {

        // This is a single-line comment

        /* This is a multi-line comment written on one line */

        System.out.println("This program is an example of comments in Java.");

        int marks = 85; // A single-line comment can also appear after code
        System.out.println("Marks: " + marks);
    }
}

Sample Input

No input required

🖥 Program Output

                                This program is an example of comments in Java.
Marks: 85
                            

📘 Explanation

Overview

Comments are notes in the source code that explain what the code does. The Java compiler ignores them, so they never affect how the program runs. Java has three types of comments, and this program uses all of them.

The comment type depends on the syntax, not the number of lines. /* ... */ is a multi-line comment even when it fits on a single line.

Step-by-Step Explanation

1

Documentation Comment

Starts with /** and is read by the javadoc tool.

/**
 * Documentation comment (Javadoc).
 * This program demonstrates the three types of comments in Java.
 *
 * @author Rumman Ansari
 */

Documentation comments are placed above classes and methods. The javadoc tool reads them to create HTML documentation. Tags such as @author add structured information.

2

Multi-Line Comment

Starts with /* and ends with */.

/* This is a multi-line comment.
 * It can span several lines.
 * The compiler ignores everything between the opening and closing markers.
 */

Everything between /* and */ is ignored, however many lines it covers. The * at the start of each middle line is optional and only there for readability.

3

Single-Line Comment

Starts with // and ends at the end of the line.

// This is a single-line comment

Use this for short notes. Anything after // on that line is ignored.

4

Multi-Line Comment on One Line

Still a multi-line comment.

/* This is a multi-line comment written on one line */

This uses the /* */ syntax, so it's a multi-line comment, even though it takes only one line.

5

Print Statement

Proves the comments don't affect output.

System.out.println("This program is an example of comments in Java.");

Only this statement and the next ones run. None of the comments above it appear in the output.

6

Comment After Code

Code runs, the comment is ignored.

int marks = 85; // A single-line comment can also appear after code
System.out.println("Marks: " + marks);

The statement before // runs normally, and the text after it is ignored. The program then prints Marks: 85.

Types of Comments in Java

Type Syntax Best Used For
Single-line // comment Short notes, or switching off one line while testing
Multi-line /* comment */ Longer explanations, or switching off a block of code
Documentation /** comment */ Describing classes and methods for the javadoc tool

Output

This program is an example of comments in Java.
Marks: 85

Common Mistakes

Nesting multi-line comments /* outer /* inner */ still outer */ doesn't work. The first */ ends the comment, and the remaining text causes a compile error.
Forgetting the closing */ If you leave out */, the rest of the file becomes a comment and you get an unclosed comment error.
Calling /* */ a single-line comment The comment type depends on the syntax. /* */ is always a multi-line comment.
Good practice Use /** */ above public classes and methods, // for short notes inside methods, and keep comments up to date when you change the code.

Generate Documentation (Optional)

javadoc -d docs MultiLineCommentExample.java

This creates a docs folder with HTML documentation built from the /** */ comments. Open docs/index.html in a browser to view it.

Key Takeaway

Java has three comment types: // for single-line notes, /* */ for multi-line notes and /** */ for documentation. The compiler ignores all of them, so they explain code without changing the output.

Complexity

Time: O(1)

Space: O(1)

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.