Subjects

Subjects

More

Documentation With Comments

Learn with content from all year groups and subjects, created by the best students.

Documentation with Comments: AP Computer Science A Study Guide



Introduction

Hello, future coding wizards! 🧙‍♂️ Today, we're diving into the wonderful world of comments and documentation in Java. Think of comments like the little Post-it notes you leave around to remind you where you stashed your snacks. Except here, they help both you and others understand your code. So, dust off your quill (or keyboard) and let's spice up our code!



Why Use Comments?

Imagine trying to solve a puzzle in complete darkness. Not fun, right? Comments illuminate your code, making it as clear as day. They don’t get compiled by the Java compiler, meaning they’re like invisible notes for humans—not computers. They help us look back at our work and understand the logic without needing a telepathic link to our past selves.



Types of Comments in Java

Java generously provides us with three types of comments: Single-line comments, multi-line comments, and Javadoc comments. Let’s dive into each type and discover their magical properties!



Single-Line and Multi-Line Comments

Single-line comments are like those quick notes you jot down on sticky notes. You start them with // and they span one line. Multi-line comments, on the other hand, are your diary entries—they can go on for multiple lines. You start them with /* and end them with */. Here, check out these chatty examples:

// This is a single line comment. Think of it as a tweet: short and sweet!

/*
 * This is a multi-line comment.
 * It can go on and on, much like your friend's elaborate vacation stories.
 * Just remember to end it with a star-slash!
 */

These types of comments are fabulous for explaining code that’s about as clear as a foggy morning, especially before loops or if-else statements. But don’t go commenting on the obvious! No need to label a potato if everyone can see it’s a potato.



Javadoc Comments

Ah, the Javadoc comments—the Hogwarts Library of your code! These special comments generate documentation, like the ones we ogled at in Unit 2. They’re placed right before classes or methods to describe what they do. Begin these with /** and end like a multi-line comment. Here’s how to conjure one:

/**
 * This is a description of what the class, method, or constructor does.
 * It starts with /** (two asterisks, no less!).
 * Within it, you also list preconditions and postconditions.
 *
 * @param arg1 description of the first argument
 * @return description of the return value
 */

Javadoc comments are as useful as having a GPS for your code—helping others (and future you) understand what each class and method does and how to use them.



Commenting Our Two Classes

Let’s sprinkle some comment magic into the two classes we’ve been working on: Assignment and Student.

/** 
 * Represents an assignment that a student will complete.
 */
public class Assignment {
  private boolean correctAnswer; // The answer to an assignment, either true or false

  /** 
   * Constructs a new assignment with one True/False question and sets the correct answer.
   * 
   * @param answer The correct answer to the question.
   */
  public Assignment(boolean answer) {
    correctAnswer = answer;
  }
}

/** 
 * Represents a high school student.
 */
public class Student {
  private int gradeLevel; // The student's grade, ranging from 9 to 12
  private String name; // The student's name, in "FirstName LastName" format
  private int age; // The student's age, must be a positive integer
  private Assignment assignment; // The current assignment the student is working on

  /** 
   * Constructs a new student with the specific grade level, name, and age.
   * 
   * @param gradeLev The grade level of the student.
   * @param fullName The full name of the student.
   * @param ageNum The age of the student.
   */
  public Student(int gradeLev, String fullName, int ageNum) {
    gradeLevel = gradeLev;
    name = fullName;
    age = ageNum;
    assignment = null; // No active assignment at the moment
  }
}


Key Terms to Review

Postconditions: After your method runs, these are the rules. Like making sure you've tied your shoelaces before walking.

Preconditions: Before you start the action, these are the rules. It's like checking if you have the right map for your treasure hunt.

Throw Statements: In Java, these let you say, “Something went wrong!” and throw an exception. It’s like raising your hand in class when you hit a snag.

Try-Catch: A way to attempt some code (try) and handle problems (catch) if they occur. Think of it as wearing a helmet before skateboarding—protection, just in case things go south.



Conclusion

Voilà! Now you’re equipped to comment your Java code like a pro and craft documentation that would impress even Gandalf. Remember, comments are your breadcrumbs, guiding anyone who reads your code—including future you. So go forth, write awesome code, and sprinkle those comments like the coding wizard you are! 💻✨

Knowunity is the # 1 ranked education app in five European countries

Knowunity was a featured story by Apple and has consistently topped the app store charts within the education category in Germany, Italy, Poland, Switzerland and United Kingdom. Join Knowunity today and help millions of students around the world.

Ranked #1 Education App

Download in

Google Play

Download in

App Store

Knowunity is the # 1 ranked education app in five European countries

4.9+

Average App Rating

13 M

Students use Knowunity

#1

In Education App Charts in 12 Countries

950 K+

Students uploaded study notes

Still not sure? Look at what your fellow peers are saying...

iOS User

I love this app so much [...] I recommend Knowunity to everyone!!! I went from a C to an A with it :D

Stefan S, iOS User

The application is very simple and well designed. So far I have found what I was looking for :D

SuSSan, iOS User

Love this App ❤️, I use it basically all the time whenever I'm studying

Can't find what you're looking for? Explore other subjects.