Unit 1 · 10 min
1.8 Documentation with Comments
Comments explain code to people; the compiler ignores them.
Summary, objectives and course and exam description (CED) codes
Comments explain code to programmers and are ignored by the compiler. Java has three types: // for one line, /* */ for a block and /** */ for Javadoc comments that create API documentation. A precondition must be true before a method runs; a postcondition must be true after it runs.
By the end of this session you can
- 1.8.A Describe the functionality and use of code through comments.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
1. Comments and their three types
Comments are written for both the original programmer and other programmers, to help them understand the code and what it does. Comments are ignored by the compiler and are not executed when the program runs 1.8.A.1.
The 3 types of comment in Java 1.8.A.1:
| Comment | Use |
|---|---|
// text |
one line |
/* text */ |
a block over several lines |
/** text */ |
Javadoc: used to create API documentation |
/* Scoreboard: adds the points for one round.
Written for the class quiz game. */
int points = 40; // points before this round
points += 15;
// points = 0; this line is a comment, so it never runs
System.out.println(points);
Output:
55
The line // points = 0; looks like code, but it is a comment, so the compiler ignores it.
2. Preconditions
A precondition is a condition that must be true just before a method runs, in order for it to behave as expected. There is no expectation that the method will check that its preconditions are satisfied 1.8.A.2. It is the caller’s job to meet them.
3. Postconditions
A postcondition is a condition that must always be true after a method runs. Postconditions describe the outcome of the method: what is returned, or the current values of the object’s attributes 1.8.A.3.
Both conditions are often written in a Javadoc comment above the method:
/** Returns the average score: total divided by count.
* Precondition: count > 0
* Postcondition: returns the average (total divided by count) as a double */
public static double average(int total, int count)
{
return (double) total / count;
}
// in main
System.out.println(average(170, 2));
System.out.println(average(10, 4));
Output:
85.0
2.5
What if a caller breaks a precondition? This method shares points equally. It does not check people, so with 0 its behavior is not what anyone wants:
/** Returns each person's share of the points.
* Precondition: people > 0
* Postcondition: returns total divided by people (int division) */
public static int share(int total, int people)
{
return total / people;
}
// in main
System.out.println(share(90, 4));
System.out.println(share(90, 0));
Output:
22
Exception in thread "main" java.lang.ArithmeticException: / by zero
The first call meets the precondition and returns 22. The second call breaks it, so the program stops with an ArithmeticException. The fault belongs to the caller who broke the precondition, not to the method.
Watch out: if a call breaks a precondition, the method’s behavior is not guaranteed. It is the caller’s job, not the method’s, to meet it.
Lock it in: memory hooks
- Pre and postPre = before the call (the caller's promise); post = after the call (the method's promise).
- Comment typesTwo slashes for a line, slash-star for a block, slash-star-star for the docs.
Common misconceptions
"A method checks its own preconditions and reports a problem."
Not quite. There is no expectation that the method will check its preconditions. Meeting them is the caller's job; if they are broken, the method's behavior is not guaranteed.
"Comments slow the program down."
Not quite. Comments are ignored by the compiler and are not executed when the program runs.
"A postcondition is what must be true before a method runs."
Not quite. That is a precondition. A postcondition must always be true after the method runs: it describes what is returned or the values of the object's attributes.
Exam traps
- If a call breaks a precondition, the method's behavior is not guaranteed. It is the caller's job, not the method's, to meet it.
- Code inside a comment does not run, even if it looks like a valid statement.
2PracticeRecall it, then check yourself
In 30 seconds
- Comments help the original programmer and other programmers understand the code; the compiler ignores them.
- // is a one-line comment, /* */ is a block comment, /** */ is a Javadoc comment used to create API documentation.
- A precondition must be true just before a method runs for it to behave as expected; the method is not expected to check it.
- A postcondition must always be true after the method runs; it describes what is returned or the values of the object's attributes.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 4 questions
Checkpoint: Documentation with Comments
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
3PrevailLock it in for exam day
By the numbers
- Types of comment three types CED 1.8.A.1
- Conditions in method documentation two conditions CED 1.8.A.2, 1.8.A.3
- preconditiontrue just before the method runs
- postconditiontrue after the method runs
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Commenttap to flip
Text in code written for programmers and ignored by the compiler. Java has //, /* */ and /** */ comments.
Example and CED codes -
Javadoc commenttap to flip
A comment that starts with /** and ends with */, used to create API documentation.
Example and CED codes -
Preconditiontap to flip
A condition that must be true just before a method runs for it to behave as expected. The method is not expected to check it.
Example and CED codes -
Postconditiontap to flip
A condition that must always be true after a method runs, describing what is returned or the values of the object's attributes.
Example and CED codes -
Compilertap to flip
A program that checks code for some errors before it can run. Errors the compiler finds must be fixed first.
Example and CED codes
Questions students ask
What are the three types of comments in Java?
// starts a comment that runs to the end of the line, /* */ encloses a block comment over several lines, and /** */ is a Javadoc comment, used to create API documentation.
What is a precondition in Java?
A precondition is a condition that must be true just before a method runs for the method to behave as expected. The method is not expected to check it; the caller must make sure it is true.
What is a postcondition?
A postcondition is a condition that must always be true after a method runs. It describes the outcome, such as the value returned or the current values of the object's attributes.