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

Java Program to Demonstrate Multi-Line Comments

3,453 Views
Java Java 8 or later
Beginner
Demonstration

Learn how to write a multi-line comment in Java using /* */. This program uses a multi-line comment to explain the code, declares an integer variable and prints its value. It also shows a comment placed inside a line of code. The compiler ignores all comments, so they don't change the output.

Before You Begin

What you will learn

  • Write a multi-line comment using /* and */
  • Write multi-line comments in the plain style and the star-prefixed style
  • Place a /* */ comment inside a line of code
  • Understand that multi-line comments can't be nested
  • Know when to use a multi-line comment instead of a single-line comment

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
  • Single-line comments using //

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named MultiLineComment.
  3. Define the main() method.
  4. Write a multi-line comment explaining the program. The compiler ignores it.
  5. Declare an integer variable number and assign it 10.
  6. Display the value of number.
  7. Write a second multi-line comment in the star-prefixed style.
  8. Calculate total = number + 5, with an inline /* */ comment inside the expression.
  9. Display the value of total.
  10. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac MultiLineComment.java
java MultiLineComment

💻 Program Code

public class MultiLineComment {

    public static void main(String[] args) {

        /* This is a multi-line comment.
           In this program, we are going to
           declare a variable and print its value in Java. */
        int number = 10;

        System.out.println(number);

        /*
         * A multi-line comment can also be written
         * with a * at the start of each line.
         * This style is easier to read.
         */
        int total = number /* inline comment */ + 5;

        System.out.println(total);
    }
}

Sample Input

No input required

🖥 Program Output

                                10
15
                            

📘 Explanation

Overview

A multi-line comment (also called a block comment) starts with /* and ends with */. The compiler ignores everything between these two markers, however many lines it covers. It's useful for longer explanations that don't fit comfortably on one line.

A multi-line comment ends at the first */ the compiler finds. That's why you can't put one multi-line comment inside another.

Step-by-Step Explanation

1

Plain Multi-Line Comment

Text spread across several lines.

/* This is a multi-line comment.
   In this program, we are going to
   declare a variable and print its value in Java. */

The comment opens with /* on the first line and closes with */ on the last line. All three lines are ignored.

2

Declare and Print a Variable

The code that actually runs.

int number = 10;
System.out.println(number);

This creates an integer variable number with the value 10 and prints it.

3

Star-Prefixed Style

The most common professional style.

/*
 * A multi-line comment can also be written
 * with a * at the start of each line.
 * This style is easier to read.
 */

The * at the start of each middle line is optional. Java treats it as part of the comment text, but it makes the comment block easier to see.

4

Inline Multi-Line Comment

A comment inside a statement.

int total = number /* inline comment */ + 5;
System.out.println(total);

Because /* */ has a clear end marker, it can sit in the middle of a line. The compiler reads this as number + 5, so 15 is printed. A // comment can't do this, because it hides everything to the end of the line.

Single-Line vs Multi-Line Comment

Feature Single-Line // Multi-Line /* */
Ends at End of the line The closing */
Covers several lines No, needs // on each line Yes
Can sit inside a statement No Yes
Best for Short notes Longer explanations, or switching off a block of code

Output

10
15

Common Mistakes

Nesting comments In /* outer /* inner */ outer */, the first */ ends the comment. The leftover outer */ causes a compile error.
Forgetting the closing */ Without */, the rest of the file becomes a comment and you get an unclosed comment error.
Adding a space inside the marker / * or * / isn't a comment marker. There must be no space between / and *.
Good practice Wrap a block of code in /* */ to switch it off while testing. Make sure that block has no */ inside it.

Key Takeaway

A multi-line comment starts with /* and ends with */. It can cover many lines or sit inside a statement, but it can't be nested. The compiler ignores it, so the output stays the same.

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.