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.

1.8.A1.8.A.11.8.A.21.8.A.3

Reviewed against the CED on

Know this already? Jump to the checkpoint.

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

  1. Comments help the original programmer and other programmers understand the code; the compiler ignores them.
  2. // is a one-line comment, /* */ is a block comment, /** */ is a Javadoc comment used to create API documentation.
  3. A precondition must be true just before a method runs for it to behave as expected; the method is not expected to check it.
  4. 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.

  1. 1 A method's documentation says Precondition: num > 0. A programmer calls the method with num equal to 0. Which is true? 1.8.A.2
    Show answer

    Answer: B. The method is not expected to check preconditions; meeting them is the caller's job. A is wrong because no check is promised. C is wrong because the compiler does not read comments or know the run-time value of num. D is wrong because Java never changes an argument to meet a precondition.

  2. 2 Consider the following code segment. What is printed? 1.8.A.1
    int count = 3; // count = 10;
    /* count = 20; */
    count = count * 2;
    System.out.println(count);
    Show answer

    Answer: A. Comments are ignored by the compiler and never run, so only count = 3 and count = count * 2 execute, giving 6. B (20) runs the one-line comment as code (10 * 2). C (40) runs the block comment as code (20 * 2). D (10) takes the value in the one-line comment and skips the multiplication.

  3. 3 Which type of comment is used to create API documentation? 1.8.A.1
    Show answer

    Answer: C. /** */ comments are Javadoc comments, used to create API documentation. A is a one-line comment and B is a block comment; both are ignored without producing documentation. D is not a Java comment at all, so it does not compile.

  4. 4 Consider the following documentation for a method of a BankAccount class. A call deposit(50) meets the precondition. Which of the following is guaranteed after the call? 1.8.A.3, 1.8.A.2
    /** Precondition: amount > 0
     *  Postcondition: the balance has increased by amount */
    public void deposit(int amount)
    Show answer

    Answer: D. A postcondition must always be true after the method runs; here it says the balance increased by amount, which is 50. A is wrong because methods are not expected to check preconditions. B is wrong because the method is void, so it returns nothing. C is wrong because the documentation says nothing about printing.

3PrevailLock it in for exam day

By the numbers

Name each list from memory, then reveal it to check. Each list shows how many items to recall.

  • Types of comment three types CED 1.8.A.1
    1. // one line
    2. /* */ a block over several lines
    3. /** */ Javadoc, used to create API documentation
  • Conditions in method documentation two conditions CED 1.8.A.2, 1.8.A.3
    1. preconditiontrue just before the method runs
    2. 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.

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure