Java Comments — When Stale Docs Cause Thread-Safety Bugs
A misleading thread-safety comment caused $50K in data corruption.
- Java has three comment types: //, / /, and /* / (Javadoc)
- // comments are for short inline notes that apply to a single line
- / / comments can span multiple lines and temporarily disable code
- /* / Javadoc comments generate API docs and power IDE tooltips
- Production risk: misleading comments cause bugs faster than missing ones
- Biggest mistake: commenting the WHAT instead of the WHY
Imagine you're building a massive LEGO set and you stick tiny Post-it notes on certain sections saying 'this bit becomes the spaceship cockpit' or 'don't change these pieces.' Comments in Java are exactly those Post-it notes — they're messages you leave inside your code for yourself or your teammates. Java completely ignores them when it runs the program; they exist purely for humans. Think of your code as a recipe and comments as the little chef's notes in the margins explaining why you add salt before the eggs.
Every professional Java codebase you'll ever open is full of them. Comments are one of the first things a senior developer looks at when they review your code — not to see if you commented everything, but to see if you commented the right things. They reveal how clearly you think, how much you care about the next person reading your work, and whether you understand what your own code is actually doing. That's a lot of weight for a few lines that the compiler throws away.
The real problem comments solve is the gap between what code does and why it does it. A machine can read your logic perfectly fine — it doesn't need explanations. But six months from now, when you come back to fix a bug at 11pm, you are not going to remember why you wrote that weird if-condition. Comments bridge that gap. They turn code from a wall of symbols into a story a human can follow.
By the end of this article you'll know all three types of Java comments, exactly when to use each one, how to write comments that actually help instead of clutter, and the specific mistakes that make experienced developers cringe when reviewing beginner code. You'll leave with habits that will make you look like a professional from day one.
Single-Line Comments — Your Quick Margin Notes
A single-line comment starts with two forward slashes: //. Everything after those two slashes on that same line is completely ignored by Java. The moment you hit Enter and move to the next line, you're back in 'real code' territory.
Use single-line comments for short, punchy explanations — things you can say in one breath. They're perfect for explaining a tricky calculation, a magic number, or a decision that isn't obvious from the code alone. If your explanation needs more than one line, you're probably reaching for the wrong tool (more on multi-line comments next).
One important habit: put the comment above the line it describes, not crammed at the far right end of a long line of code. Comments placed at the end of a line — called inline comments — are fine for very short labels, but if the comment is longer than about 30 characters it gets hard to read. Your future self will thank you for keeping things clean.
Notice in the example below that we don't comment every single line. We only comment where the logic needs explanation. Over-commenting is its own kind of noise — if the code already tells the story clearly, a comment just repeats it.
// add 1 to counter above counter++ tells us nothing we can't already see. Writing // skip index 0 because the header row is not actual data tells us something only you know. That's where comments earn their keep.counter++ for increment)1.8 for Celsius-Fahrenheit ratio)Multi-Line Comments — When One Line Isn't Enough
Sometimes you need more space — to explain a whole block of logic, describe the context of a method, or temporarily disable a chunk of code while debugging. That's what multi-line comments are for. They start with / and end with /, and everything in between is ignored by Java, whether it's two lines or two hundred.
A very common use case is 'commenting out' code during development. Say you wrote a calculation two different ways and you want to test one while keeping the other around. Wrap the one you're not testing in / ... / and Java pretends it doesn't exist. Just remember to clean this up before you commit your code — commented-out code that ships to production is a red flag in code reviews.
Another solid use for multi-line comments is a block at the top of a file or a complex method explaining what the code is trying to achieve overall. Think of it like the introduction paragraph of an essay — give the reader the big picture before they dive into the details.
One style note: many Java developers decorate multi-line comments with a leading asterisk on each line (the * pattern you see in the example). Java doesn't require this — those asterisks are just part of the text being ignored. But it's a widely accepted convention that makes the comment boundaries visually obvious.
/ outer / inner / /, Java sees the first / and thinks the comment is over — then it tries to parse / as code and throws a compile error. If you need to comment out a block that already contains a multi-line comment, use your IDE's block-comment shortcut (Ctrl+/ or Cmd+/) which adds // to each line instead./ ... / around code that's been disabled for weeks, then someone accidentally uncomments it and deploys broken logic./ / comments. But remove before committing.Javadoc Comments — The Professional Documentation Standard
Javadoc comments are the third type, and they're in a league of their own. They look like multi-line comments but start with /** (two asterisks) instead of one. Java's built-in documentation tool — called Javadoc — reads these special comments and automatically generates a professional HTML documentation website from them. This is exactly how the official Java documentation at docs.oracle.com was created.
You write Javadoc comments directly above a class, a method, or a variable you want to document. Inside them you use special tags that start with @ to describe specific things: @param documents a parameter the method accepts, @return describes what the method gives back, and @author records who wrote it.
Here's the key insight beginners miss: Javadoc comments aren't just for open-source libraries or huge enterprise projects. If you're writing a method that another developer (or future you) will call, a Javadoc comment means your IDE will show that documentation as a tooltip the instant someone types your method name. IntelliJ, Eclipse, VS Code — they all do this automatically. It's one of the highest-leverage habits you can build early.
For now at the beginner level, focus on documenting your public methods — the ones other code will call. Don't stress about documenting every private helper method until you have the habit down.
//, / /, and /* / in Java?' The answer that impresses is: the first two are purely for human readers and are discarded by the compiler, while Javadoc comments (/* /) are processed by the Javadoc tool to generate API documentation — and are also picked up by IDEs to show hover tooltips. That distinction between compile-time and documentation-time is what separates a memorized answer from an understood one.Writing Comments That Survive Code Reviews
You've written a comment. Great. But will it survive a senior developer's review? Here's what separates helpful comments from clutter:
- State the intention, not the mechanics. Instead of
// loop through list and add to total, write// calculate sum of all active orders for this customer. - Keep them close to the code they describe. Comments that drift far from the relevant lines become orphaned and misleading when the code is refactored.
- Avoid adverbs like 'obviously' or 'clearly'. If it's obvious, you don't need a comment. If it's not, the comment should explain, not preface.
- Use TODO and FIXME consistently. Many IDEs collect these into a task list. But don't leave them in production — a TODO in a commit should be a commit message, not a comment.
- Match the team's style. If the team uses Javadoc for every public method, do it. If they prefer high-level only, follow suit. Consistency matters more than your personal preference.
- A good comment answers the question 'why does this code exist?' that the code itself cannot answer.
- A bad comment repeats the code in English, doubling the reading effort.
- A dangerous comment contradicts the actual code — it's a trap waiting to trigger a bug.
Javadoc Pitfalls and CI Integration
Even well-intentioned Javadoc can cause problems if you don't handle it right. Here are the common pitfalls and how to catch them automatically.
Pitfall 1: Out of sync Javadoc — You change a method signature but forget to update the Javadoc. Now the tooltip shows wrong parameter names or types. Fix: Use -Xdoclint:missing during Javadoc generation to report all mismatches.
Pitfall 2: HTML in Javadoc — Javadoc supports HTML tags like <p>, <code>, <pre>. But if you close a tag incorrectly, the generated HTML can break the page layout of your documentation site. Fix: Validate Javadoc HTML output with a tool like htmlhint or use -taglet to check for common mistakes.
Pitfall 3: No Javadoc for public methods — In a large team, someone will inevitably forget. Fix: Enforce this in CI. Use Maven's checkstyle plugin with a rule that fails the build if a public method lacks Javadoc.
Pitfall 4: Unlinked references — Using {@link OtherClass} that doesn't resolve causes a warning but builds succeed. This breaks the generated docs because the link becomes dead text. Fix: Run Javadoc with -linksource and verify all links by checking the output for warning patterns.
-Xdoclint:missing warnings, not on stylistic ones. That catches out-of-sync comments without blocking developers for minor formatting issues.The \$50,000 Misleading Comment
- Never trust a comment that claims something about thread safety without reading the code.
- If a comment doesn't match the code, it's worse than no comment at all.
- Use automated tools (like PMD or SpotBugs) to catch stale comments against code patterns.
Key takeaways
// for single-line, / / for multi-line, and /* / for Javadoc — each with a distinct purpose, not just stylistic preference./* / comments to generate browsable HTML documentation and power IDE tooltips./ / multi-line comments// per-line comments or your IDE's block-comment shortcut as the safe alternative.Common mistakes to avoid
4 patternsTrying to nest multi-line comments
/ / inner comment / outer still going / causes a compile error because Java closes the comment at the first / it finds, leaving outer still going / as invalid code.// single-line comments inside a multi-line block, or use your IDE's line-comment shortcut (Ctrl+/ or Cmd+/) which prefixes each line with // instead.Commenting WHAT the code does instead of WHY
// multiply radius by radius above radius * radius is pure noise that doubles the reading effort with zero benefit. A reader can see the multiplication — they can't see your reasoning.// using radius² because the area formula requires squaring, not doubling. Always ask yourself: 'would someone reading this code already know this from the code alone?' If yes, delete the comment.Leaving dead commented-out code in production commits
/ old code here / committed to a shared codebase confuse teammates, clutter diffs, and suggest you don't trust version control to preserve history.Using Javadoc tags incorrectly (e.g., @param name instead of @param name description)
@param radius the radius of the circle in centimeters.Interview Questions on This Topic
What are the three types of comments in Java, and what is the key difference between a multi-line comment and a Javadoc comment?
// (ends at line break), multi-line / / (spans multiple lines, ends with /), and Javadoc / / (multi-line but processed by the Javadoc tool to generate HTML documentation and provide IDE tooltips). The key difference: multi-line comments are completely ignored by everything except the human reader, while Javadoc comments are parsed by the javadoc command-line tool and IDEs to produce formal documentation.Frequently Asked Questions
That's Java Basics. Mark it forged?
6 min read · try the examples if you haven't