What kinds of comments does Java support, and what is the syntax for each?
answer
- // to end of line
- /* ... */ block, any length
- /** ... */ = Javadoc, extra asterisk
- All stripped at lexing, no bytecode
- Javadoc read by the javadoc tool
basics
~20 sJava 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 sJava 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
Name all three forms and write the correct delimiters for each.
Explain that all three are stripped at lexing and that Javadoc is special only to the javadoc tool, plus common Javadoc tags.
Discuss when to use each, Javadoc tag conventions, and that comments never reach bytecode.
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