back to course
Lesson 04 / 805%· free preview
Getting Started4/4
Java Comments
Single-line, multi-line, and JavaDoc comments — when and how to use each.
What are Comments?
Comments are notes for humans reading your code — the compiler ignores them. Java has three flavours.
1. Single-line — `//`
java// This is a single-line comment int x = 5; // can also follow code
2. Multi-line — `/* ... */`
java/* This block can span as many lines as you need. */
3. JavaDoc — `/** ... */`
Special multi-line comments parsed by the javadoc tool to generate API docs.
java/** * Returns the area of a circle. * @param r radius (must be non-negative) * @return the computed area */ public double area(double r) { return Math.PI * r * r; }
Example
javapublic class Main { // entry point public static void main(String[] args) { /* multi-line: prints a friendly greeting */ System.out.println("Hello!"); // greeting } }
Hello!
When to comment
- ✅ Why — context the code can't express.
- ✅ TODO / FIXME markers for follow-ups.
- ✅ JavaDoc on every public class & method.
- ❌ Restating what the code already says clearly.
Common Mistakes
- Commenting out big blocks of code instead of deleting (use Git).
- Stale comments — they lie when the code changes and the comment doesn't.
Practice Exercises
- Add a JavaDoc comment to your
add(int, int)method describing the params and return value. - Run
javadocon your file and open the generated HTML to see your comments rendered as API docs.
AI-powered recap
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
# program
Program
Java
/**
* A tiny JavaDoc demo.
*/
public class Main {
// single-line
public static void main(String[] args) {
/* multi-line */
System.out.println("Documented!");
}
}Ready to move on?
// example library
Want more hands-on snippets in Java?
Browse 100 runnable examples · across 10 chapters · short, copy-paste-friendly · grouped by topic
// side-by-side reference
See this in other languages
Compare the same concept across C, C++, Java, and Python — one table, zero tab-switching.
// feedback.matters()
Did this lesson help you?
