Javadoc
A comment style that explains what your code does — written for the next person who reads it, who might just be you, later.
/** * Counts how many vowels appear in a phrase, ignoring case. * * @param phrase the text to check * @return the number of vowels found */ static int solve(String phrase) { ... }
Three Comment Types
Java gives you three ways to leave a note in your code. Each one exists for a different job.
Line comment
// checks each vowel one at a time
Starts with //, ends at the end of the line. Good for a quick note on one specific piece of code.
Block comment
/* This whole method works by
comparing string lengths
before and after removing
each vowel. */
Starts with /*, ends with */. Can span multiple lines — good for a longer explanation.
Javadoc comment
/**
* @param phrase text to check
* @return vowel count
*/
Starts with /** — one extra star. Sits directly above a method, and can generate real documentation pages automatically.
Preconditions & Postconditions
Good documentation doesn't just describe what a method does — it describes what has to be true before you call it, and what's guaranteed once it's done.
| Term | Means |
|---|---|
| precondition | what must be true before the method is called, for it to work correctly |
| postcondition | what's guaranteed to be true after the method finishes |
For solve(String phrase) above: the precondition is that phrase isn't null — calling .toLowerCase() on null would crash. The postcondition is that the returned number is never negative, since you can't have fewer than zero vowels.
Before & After
The exact same code, correct either way — but only one version tells you anything before you've read every line.
static String solve(String word) { return word.charAt(0) + "" + word.charAt(word.length() - 1); }
/** * Combines a word's first and * last character. * * @param word the word to shorten * @return the first and last * letter, concatenated */ static String solve(String word) { return word.charAt(0) + "" + word.charAt(word.length() - 1); }
Running javadoc
Writing the comment is only half of it. javadoc is a real command-line tool that reads every Javadoc comment in a file and builds an actual browsable HTML documentation page from it — the same way official Java library docs get made.
That "1 warning" at the end is normal — javadoc often flags small things like a missing package declaration in a single-file project. It doesn't stop the docs from generating.
A whole folder of new HTML files appears next to your
.java file. index.html is the entry point — open that first, then click through to your class's own page (named after your class, like vowelcount.html) to see your @param and @return text rendered as an actual formatted reference page.
If a class has more than one method — including overloaded methods that share a name — each one gets listed separately on that class's page, with its own description, right below the others.