skip to content

Comments

Line, block and Javadoc comments, plus the two edge rules: block comments do not nest, and comment delimiters inside string literals are just characters. Mostly trivia, but the non-nesting rule bites in real code.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

4

What kinds of comments does Java support, and what is the syntax for each?

level: juniorimportance: must knowfreq 70%

answer

  1. // to end of line
  2. /* ... */ block, any length
  3. /** ... */ = Javadoc, extra asterisk
  4. All stripped at lexing, no bytecode
  5. Javadoc read by the javadoc tool

basics

~20 s

Java has three: single-line starting with //, multi-line wrapped in /* ... /, and Javadoc written as /* ... */ which is a documentation comment the javadoc tool reads. All of them are ignored by the compiler.

solid answer

~40 s

Java supports three comment forms. A single-line comment starts with // and runs to the end of the line. A multi-line (block) comment is everything between /* and */ and can span many lines. A Javadoc comment is a special block comment that opens with /** and closes with */; it is placed directly above a class, method, or field and is consumed by the javadoc tool to generate API documentation. To the compiler all three are equivalent: they are stripped during lexical analysis and produce no bytecode. The difference between a plain block comment and Javadoc is purely the extra asterisk and the tooling convention around it. Use // for short notes, /* */ for longer prose, and /** */ when you want documented, published API.

go deeper

for a junior

Name all three forms and write the correct delimiters for each.

for a middle

Explain that all three are stripped at lexing and that Javadoc is special only to the javadoc tool, plus common Javadoc tags.

for a senior

Discuss when to use each, Javadoc tag conventions, and that comments never reach bytecode.

for a principal

Frame comment/doc strategy as part of API maintainability — Javadoc as the contract surface, doc-as-code, and avoiding redundant comments.

### What a comment is A *comment* is text inside source code that the **compiler ignores**. It exists only for human readers (and, in one case, for a documentation tool). Comments produce no machine instructions and have zero effect on how the program runs. ### The three forms in Java **1. Single-line comment: `//`** Everything from `//` to the end of that physical line is a comment. ```java int x = 5; // this explains x ``` The comment ends at the newline; the next line is normal code again. **2. Multi-line / block comment: `/* ... */`** Everything between the opening `/*` and the next closing `*/` is a comment, even across many lines. ```java /* This can span several lines. */ ``` It does not have to span multiple lines — `/* short */` is also valid mid-line. **3. Javadoc / documentation comment: `/** ... */`** A block comment whose opener has an **extra asterisk** (`/**`). It is meant to sit immediately above a declaration (class, interface, method, field) and describe it. The `javadoc` tool that ships with the JDK reads these and produces HTML API docs. It supports tags like `@param`, `@return`, `@throws`, and `@deprecated`. ```java /** * Adds two numbers. * @param a first addend * @param b second addend * @return the sum */ int add(int a, int b) { return a + b; } ``` Importantly, `/** ... */` is still just a block comment to the *compiler* — the special meaning is only honored by tooling. ### How the compiler treats them During **lexical analysis** (the first phase, which turns characters into tokens), all three comment kinds are recognized and discarded. None reach later compilation phases, so none appear in the `.class` bytecode. (Javadoc text is recovered by the separate javadoc tool reading the *source*, not the bytecode.) ### Choosing which to use - `//` — brief inline notes, temporarily disabling a line. - `/* */` — longer explanations, or commenting out a block. - `/** */` — public, published documentation of an API element.

  • Does a Javadoc comment change the compiled bytecode?
    No. To the compiler /** */ is an ordinary block comment that is stripped at lexing. Only the separate javadoc tool reads it (from the source) to build HTML docs.
  • Can you put // inside a line of code and still have code after it on the next line?
    Yes. // only comments to the end of its own line; the following lines are normal code.

saying these in an interview costs you the question

  • Thinking Javadoc affects how the code runs — it does not, it is only read by tooling
  • Believing the compiler treats /** */ specially; only the javadoc tool does
  • Claiming a // comment can span multiple lines

context

open as a page

Why can't block comments be nested in Java, and what happens if you try?

level: middleimportance: should knowfreq 55%

basics

~20 s

A block comment ends at the very first / the compiler sees. So if you put one / */ inside another, the inner */ closes the whole thing, and the leftover text after it becomes real code that usually fails to compile.

open as a page

When should you write a comment in Java, and when does a comment signal a deeper code-quality problem?

level: middleimportance: should knowfreq 50%

basics

~20 s

Comment to explain WHY, not WHAT. If a comment just restates the code, rename things or extract a method instead. Use Javadoc for public APIs. Comments that explain confusing code often mean the code should be made clearer.

open as a page

Why is a comment delimiter like // or /* ignored when it appears inside a string literal?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Inside a string the characters are just data, not source syntax. The lexer knows it is reading a string, so it does not treat // or /* as comment starters. So System.out.println("http://x") prints the whole URL.

open as a page