Java block comments start with /* and end with */ so notes can span several lines of code. See how they differ from // line comments, when /** is used for documentation, and why clear comments help explain a method or temporarily set code aside.

Multiple Choice

How is a Block Comment initiated in Java?

A block comment in Java is initiated with the symbol combination "/*" and it is terminated with "*/". This allows for multi-line comments to be easily included within the code, making it useful for providing extensive explanations, documenting code sections, or temporarily disabling code during development. Block comments can span several lines, which distinguishes them from single-line comments that are marked with "//" and only apply to the line they are on. Additionally, while /** is a specific form used for documentation comments (which can be processed by tools to generate documentation), it still follows the same initiation with /*. The "#" symbol is not recognized as a comment marker in Java. Thus, using "/*" appropriately signifies the beginning of a block comment, making it the correct answer.

Java code can explain itself, and comments are one of the simplest ways to do that. A block comment begins with /* and ends with */. Anything between those markers is treated as a comment rather than as code, so the comment can stretch across several lines. That makes block comments handy for notes that need more room than a short, line-by-line remark.

The markers that make a block comment

A block comment has a clear beginning and a clear end:


/*

* This method checks whether a number

* is within the allowed range.

*/

The opening marker is /*; the closing marker is */. Java ignores the text between them when it compiles the program. The asterisk at the start of each line is optional. Developers often add it because it makes a longer comment easier to scan, but the compiler doesn’t require it.

For example, this is also valid:


/*

This comment has no extra asterisks.

It still spans more than one line.

*/

Think of the markers as bookends. The opening one tells Java, “The comment starts here.” The closing one says, “The code resumes here.” If the closing marker is missing, Java may treat everything after the opening marker as part of the comment, often causing errors later in the file.

How it differs from //

Java also supports single-line comments, which begin with //:


int total = price * quantity; // Calculate the item total

A single-line comment ends at the end of that line. It’s a natural fit for a brief note beside a statement or for a short explanation above one.

A block comment, by contrast, can cover several lines—or just one, if that suits the situation:


/* The value is stored in cents. */

int priceInCents = 1299;

The choice is mostly about readability. A quick note may sit neatly after a line of code. A longer explanation usually deserves its own space. Neither marker makes a comment more “important”; they simply work in different ways.

There’s one subtle point worth knowing: Java does not support nested block comments. If a block comment starts with /*, the next */ closes it, even if you intended that marker to belong to another comment inside it. For instance, adding a block comment inside an existing block comment can end the outer comment too early. When you need to temporarily hide a small section of code, line comments are often a safer choice.

What about /**?

The marker /** begins a documentation comment, often called a Javadoc comment. It looks much like a regular block comment, but it has a special role: Java’s documentation tools can read it and produce reference pages.


/**

* Returns the area of a rectangle.

*

* @param width the rectangle's width

* @param height the rectangle's height

* @return the area, calculated as width times height

*/

public double area(double width, double height) {

return width * height;

}

A Javadoc comment still uses /* at its start and */ at its end. The extra asterisk signals a documentation style that tools such as the JDK’s javadoc utility can process. It’s commonly placed above public classes, methods, and fields when other developers need a clear description of how to use them.

A regular block comment is different in purpose. It can explain an implementation detail, mark a temporary note, or describe a section of code. Javadoc, on the other hand, is meant to describe a program element in a form that can become published documentation. The distinction is useful: one comment talks to someone reading the source; the other can also become part of a project’s reference material.

Comments don’t change what the program does

Comments help people understand code, but Java does not run them as instructions. Consider this example:


/*

* Start with a value of zero.

*/

int count = 0;

The program uses the assignment int count = 0;. The words in the comment have no effect on the variable. That’s why a comment can be helpful without changing the program’s behavior—and why a misleading comment can cause real confusion. If the code changes but its explanation doesn’t, readers may trust an outdated note and misunderstand what’s happening.

A useful comment adds information that the code alone doesn’t make obvious. It might explain why a particular limit exists, what a tricky calculation represents, or why a method handles an unusual case. It usually doesn’t need to repeat a simple statement in English:


// Add one to count

count++;

That note tells readers very little they couldn’t already see. A better comment might explain the reason for the change:


// Reserve one slot for the trailing newline in the output buffer.

count++;

Even then, comments shouldn’t become a substitute for clear names or well-structured code. A method named calculateMonthlyPayment needs less explanation than a method named doThing. Clear code and useful comments work together, like labels on drawers: the labels help most when the contents are sensibly arranged.

Temporarily setting code aside

Developers sometimes use block comments to disable a piece of code while they investigate a problem:


/*

sendEmail(customer);

updateOrderStatus(order);

*/

This can be convenient for a short, temporary change. But it’s easy to forget that the disabled code is still sitting there, and larger commented-out sections can make a file harder to read. Version-control tools such as Git are usually a better place to keep old code. Git records changes, so developers can review or restore earlier versions without leaving unused fragments in the current source file.

There’s also a technical wrinkle: because block comments cannot nest, wrapping a block around code that already contains /* ... */ may close the new comment earlier than expected. Line comments avoid that particular issue, since each // only applies to the rest of its own line.

A note on #

Java doesn’t use # as a comment marker in its source code. That symbol has other uses in some languages and tools, which can make it seem familiar, but it won’t start a Java comment. If a # appears where Java expects source code, the compiler will generally report an error.

The comment markers to remember are straightforward: // for a single line, /* ... */ for a block, and /** ... */ for documentation. In day-to-day Java code, these small symbols make a meaningful difference. Used with care, comments give a program context—just enough guidance to help the next person, or your future self, understand not only what the code does, but why it’s there.