Java Comments and Javadoc
Table of Contents + β
In the last lesson you learned about Java program structure. Along the way you met comments for a moment. Now letβs look at them properly.
π€ Why comments matter
You write clever code today. Three months later you open it and think, βwhat was I doing here?β That is the pain comments solve.
A comment is a note for humans, not the computer. Java skips right over it, so it never runs.
- Code says what. Comments say why, which the code alone cannot show.
- The next reader is often you. A short note now saves a confused hour later.
- Teams share files. Comments let people understand your work without asking you.
Java gives you three kinds.
βοΈ Single-line comments
The single-line comment starts with two slashes, //. Everything from there to the end of that line is ignored.
Here it is on its own line and at the end of a line of code.
// This whole line is a note for humans.int score = 90; // You can also add a note after code, like this.- It stops at the end of the line. The next line is normal code again.
- It works in two spots: on its own line, or at the end of a code line.
- It is the most common kind. Reach for it for any short note.
π Multi-line comments
When a note runs past one line, use the multi-line comment. It starts with /* and ends with */. Everything between is ignored, even across many lines.
Here it explains the block of code below it.
/* This program works out the final price. It adds tax, then takes off any discount. Riya asked for this calculation last week.*/double finalPrice = (basePrice * 1.1) - discount;- It spans as many lines as you like. Open with
/*, close with*/. - The
*people add on each line is just for looks. Java does not need it. - Forget the closing
*/and Java treats the rest of your file as one big comment, so your code stops working.
π Documentation comments (Javadoc)
The documentation comment looks like a multi-line comment but opens with /**, two stars instead of one. It is called a Javadoc comment, and it is more than a note.
You place it right above a class or method to describe what it does.
/** * Adds two whole numbers and gives back the total. */public int add(int a, int b) { return a + b;}What makes it different from a normal comment:
- It describes the class or method right below it, so the reader sees the description first.
- A tool can read it. Java ships with a program called
javadocthat turns these comments into a neat HTML page. - The two-star
/**opening tellsjavadocβthis is documentation, include itβ.
So a Javadoc comment can grow up into a real web page. Now the tags that make it powerful.
π·οΈ Javadoc tags
A tag is a special word starting with @. It marks one piece of information in a standard way, so javadoc knows exactly what it is. The common ones:
@paramdescribes one input. Write one line per input.@returndescribes what the method gives back.@authornames who wrote the code. It usually goes above a class.
Here is a fully documented method.
/** * Works out the area of a rectangle. * * @param width the width of the rectangle * @param height the height of the rectangle * @return the area, found by multiplying width and height * @author Alex */public int area(int width, int height) { return width * height;}- The first sentence says what the method does.
- Each
@paramexplains one input. @returnsays what comes back, and@authorcredits the writer.
Now run the javadoc tool on your file from the command line.
javadoc Rectangle.javaIt reads your /** */ comments, picks out the tags, and builds an HTML page showing each method, its inputs, and its return value.
You have used Javadoc already
Every time you look up how a Java method works on the official docs site, you are reading Javadoc. Those pages were built from /** */ comments in Javaβs own source code. So when you write Javadoc, you are documenting your code the same way the Java team documents theirs.
π Good comments vs bad comments
Not all comments help. The golden rule: explain why, not what. The code already shows what it does, so a comment that repeats it in English is just noise.
Look at this weak comment.
// β Bad: this just repeats what the code plainly saysi = i + 1; // add one to iAnyone can see it adds one to i. Now a comment that earns its place.
// β
Good: explains the reason, which the code cannot showi = i + 1; // skip the header row before reading the dataYou could never guess that reason from the code alone. That is the whole point.
- Say why, not what. The reader can already see what the line does.
- Do not state the obvious. A
// the main methodnote abovemainwastes time. - Let clear code speak. A well-named variable like
discountedPriceoften removes the need for a comment.
π« Commenting out code
To make Java skip a line without deleting it, put // in front of it. This is called commenting out code, and it is handy while testing.
Both styles turn code off below.
System.out.println("This line runs.");// System.out.println("This line is switched off for now.");
/*System.out.println("These two lines");System.out.println("are both switched off.");*/Remove the slashes later to switch the line back on. One warning: do not leave large blocks of dead, commented-out code in your final work. It clutters the file and confuses readers. Bring it back or delete it for good.
β οΈ Common Mistakes
Two comment habits cause real trouble.
First, over-commenting. Adding a comment to every line, even obvious ones, buries the few useful comments in noise.
// β Wrong: a comment on every obvious lineint age = 25; // set age to 25age = age + 1; // add one to ageSystem.out.println(age); // print age// β
Right: let clear code speak, comment only where it helpsint age = 25;age = age + 1; // a birthday just passed, so bump the ageSystem.out.println(age);Second, the stale comment. It was true once, but the code changed and nobody updated the note. Now it lies, which is worse than no comment at all.
// β Wrong: the comment says 10% but the code uses 20%// Apply a 10% discountdouble price = basePrice * 0.80;// β
Right: the comment matches what the code actually does// Apply a 20% discountdouble price = basePrice * 0.80;When you change code, check the comment beside it. A wrong comment sends the next reader down the wrong path.
β Best Practices
- Explain why, not what. Save comments for the reason, not a replay of the line.
- Keep comments up to date. A stale comment is worse than none.
- Prefer clear names over comments. A good name often makes a comment unnecessary.
- Use Javadoc for shared code, with
@paramand@returntags. - Do not leave dead code commented out. Bring it back or delete it.
π§© What Youβve Learned
Now you can write notes that help the next reader. Letβs recap.
- β A comment is a note for humans that Java ignores. It never runs.
- β
Single-line comments start with
//and cover the rest of that line. - β
Multi-line comments sit between
/*and*/and can span many lines. - β
Javadoc comments start with
/**and describe a class or method. - β
Javadoc tags like
@param,@return, and@authormark information in a standard way, and thejavadoctool turns them into HTML docs. - β Good comments explain why, not what, and never state the obvious.
- β You can comment out code to switch it off while testing.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does a comment do when the program runs?
Why: A comment is a note for humans. Java reads it, skips over it, and never runs it.
- 2
How do you start a Javadoc documentation comment?
Why: A Javadoc comment starts with /** (two stars). The javadoc tool reads these to build HTML docs.
- 3
Which Javadoc tag describes what a method gives back?
Why: @return describes the value a method returns. @param describes an input, and @author names the writer.
- 4
What makes a good comment?
Why: Good comments explain why, not what. The code already shows what it does, so the reason is the useful part.
π Whatβs Next?
You can now write clear notes and even proper documentation for your code. Next letβs see what actually happens when you turn your Java file into a running program, step by step.