By WebNest Studio Engineering Team
Java - Core Tutorial
Java Comments
Comments are notes in source code that the compiler ignores. They exist purely to communicate intent, context, and reasoning to human readers — including your future self. Java supports three comment styles.
Why it matters
Code is read far more often than it is written, usually by someone who was not there when the decision was made. A good comment records the reason that cannot be seen in the code; a bad one repeats the code and then goes out of date. Javadoc comments also become the documentation other developers see in their editor when they call your method.
Single-line, Multi-line, and Javadoc Comments
A single-line comment starts with // and continues to the end of the line. A multi-line (block) comment is wrapped between /* and */ and can span multiple lines. A Javadoc comment starts with /** and is used to generate HTML API documentation with the javadoc tool; it supports tags like @param, @return, and @throws.
What Makes a Good Comment
Professional Java code favors comments that explain "why," not "what" — good naming already communicates what code does. A comment should capture a non-obvious constraint, a workaround for a specific bug, or a business rule that isn't visible from the code itself. Comments that just restate the code in English go stale and mislead readers when the code changes but the comment doesn't.
Example
All three comment styles, including a Javadoc block
public class CommentsDemo {
// Discount is capped at 50% per company policy, not a technical limit.
static final double MAX_DISCOUNT = 0.50;
/*
* This method is intentionally simple: pricing rules are being
* redesigned next quarter, so no extra abstraction is added yet.
*/
static double applyDiscount(double price, double discount) {
double capped = Math.min(discount, MAX_DISCOUNT);
return price - (price * capped);
}
/**
* Prints the final price after applying a capped discount.
*
* @param price original price before discount
* @param discount requested discount as a fraction (0.0 to 1.0)
*/
public static void main(String[] args) {
double price = 100.0;
double discount = 0.75; // requested above the cap, will be limited
System.out.println(applyDiscount(price, discount));
}
}
50.0
Common Mistakes
- Writing comments that just repeat the code ("// increment i by 1" above "i++;") instead of explaining reasoning.
- Leaving outdated comments after refactoring code, which actively misleads future readers.
- Using block comments to "comment out" large chunks of dead code instead of deleting it — version control already preserves history.
Exercise
Document the method below with a Javadoc comment. It should have a one-sentence description, a @param tag for each parameter and a @return tag. The program's behaviour does not change.
Expected output: 9
public class MathUtil {
// TODO: replace this line with a Javadoc comment
static int max(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
System.out.println(max(4, 9));
}
}
Show hints
- A Javadoc comment starts with
/**, not/*, and sits directly above the method. - Describe what the caller gets back, not how the method works internally.
Show solution
public class MathUtil {
/**
* Returns the larger of two integers.
*
* @param a the first value
* @param b the second value
* @return the greater of a and b; either one if they are equal
*/
static int max(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
System.out.println(max(4, 9));
}
}
Quick quiz
1. Which comment style does the javadoc tool turn into documentation?
- A. /** ... */
- B. /* ... */
- C. // ...
- D. # ...
Show answer
A. Only comments that begin with /** are processed by javadoc.
2. Can block comments be nested, as in /* outer /* inner */ still outer */?
- A. Yes, to any depth
- B. No, the comment ends at the first */
- C. Only inside methods
- D. Only in Javadoc comments
Show answer
B. Block comments do not nest. The first */ closes the comment, and the remaining text causes a compile error.
3. How do comments affect the compiled .class file?
- A. They make it larger
- B. They slow the program down
- C. They are discarded by the compiler and have no effect
- D. They are stored as strings
Show answer
C. The compiler ignores comments entirely, so they add nothing to the bytecode.
Key Points to Remember
- Three styles: // single-line, /* ... */ multi-line, and /** ... */ Javadoc.
- Good comments explain why, not what — the code itself should explain what.
- Javadoc comments can be turned into HTML documentation using the javadoc tool.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering