Unit 1 · 10 min

1.7 Application Program Interface (API) and Libraries

You do not write everything yourself; you use classes other programmers wrote, guided by their documentation.

Summary, objectives and course and exam description (CED) codes

A library is a collection of classes, and an application programming interface (API) specification tells programmers how to use those classes. Classes are grouped into packages, and each class defines a reference type. Attributes are the data of a class, stored in variables; behaviors are what instances can do, defined by methods.

By the end of this session you can

  • 1.7.A Identify the attributes and behaviors of a class found in the libraries contained in an API.

1.7.A1.7.A.11.7.A.2

Reviewed against the CED on

Know this already? Jump to the checkpoint.

1LearnThe notes, the traps and how to remember them

1. Libraries, APIs and packages

You do not write every class yourself. A library is a collection of classes. An application programming interface (API) specification informs the programmer how to use those classes. To understand what attributes and behaviors a class in the API has, you read its documentation 1.7.A.1.

  • A class defines a specific reference type. String is a class, so String is a reference type 1.7.A.1.
  • Classes in APIs and libraries are grouped into packages 1.7.A.1.
  • Existing classes and class libraries can be used to create objects 1.7.A.1.

Example: String and Math are classes in the java.lang package, part of the library that comes with Java. The Java Quick Reference tables in topics 1.11 and 1.15 are short extracts of their API documentation.

2. Using a class you did not write

You can use a library class by reading its documentation. You do not need to see its code.

String city = "Lisbon";
System.out.println(city.length());
System.out.println(Math.sqrt(81.0));

Output:

6
9.0

The String documentation says length() returns the number of characters. The Math documentation says sqrt returns a square root. Neither class was written by you.

3. Attributes and behaviors

Attributes are the data that belong to a class; they are kept in variables. Behaviors are the actions an instance of the class can do, or that can be done to it; methods define them 1.7.A.2.

Example: a Plant class might have the attributes name and height and the behaviors grow, getName and getHeight. Its documentation could look like this:

Documentation entry Attribute or behavior?
String name: the plant’s name attribute (data in a variable)
int height: the height in centimeters attribute (data in a variable)
void grow(int cm): adds cm to the height behavior (defined by a method)
int getHeight(): returns the height behavior (defined by a method)
String getName(): returns the name behavior (defined by a method)

Using the behaviors changes or reports the attributes:

Plant fern = new Plant("Fern", 30);
fern.grow(5);
System.out.println(fern.getName() + " is " + fern.getHeight() + " cm");

Output:

Fern is 35 cm

Watch out: attribute = noun stored in a variable (height); behavior = verb defined by a method (grow). In documentation, a method name is followed by parentheses.

Lock it in: memory hooks

  • Attribute vs behaviorAttribute = noun stored in a variable; behavior = verb defined by a method.
  • Library vs APIThe library is the toolbox; the API is the instruction sheet.

Common misconceptions

  • "You have to write every class you use."

    Not quite. Existing classes and class libraries can be used to create objects. String and Math come with Java, and their API documentation tells you how to use them.

  • "A method is an attribute because it belongs to the class."

    Not quite. Attributes are data stored in variables (nouns, such as height). Behaviors are actions defined by methods (verbs, such as grow).

  • "An API is a program you run."

    Not quite. An API specification is documentation. It informs the programmer how to use the classes in a library.

Exam traps

  • Attribute = noun stored in a variable (height); behavior = verb defined by a method (grow).
  • A method name is followed by parentheses in documentation, such as grow(int cm). A variable such as height has no parentheses.

2PracticeRecall it, then check yourself

In 30 seconds

  1. A library is a collection of classes.
  2. An API specification is documentation that tells programmers how to use those classes.
  3. A class defines a specific reference type; classes are grouped into packages.
  4. Existing classes can be used to create objects.
  5. Attributes: data related to a class, stored in variables. Behaviors: what instances can do, defined by methods.

Cover this list, say the points out loud, then take the checkpoint without scrolling back up.

Checkpoint · 4 questions

Checkpoint: Application Program Interface (API) and Libraries

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 The API documentation for a Robot class shows a variable batteryLevel and a method moveForward(). Which statement is correct? 1.7.A.2
    Show answer

    Answer: D. Data stored in a variable is an attribute; an action defined by a method is a behavior. A swaps the two. B is wrong because moveForward() is a method, so it defines a behavior. C is wrong because batteryLevel is data in a variable, not an action.

  2. 2 Which of the following best describes the relationship between a library and an application programming interface (API) specification? 1.7.A.1
    Show answer

    Answer: A. Libraries are collections of classes, and an API specification informs the programmer how to use those classes. B is wrong because an API specification is documentation, not code. C is wrong because a library holds many classes, grouped into packages. D is wrong because the two are different things: the classes and their documentation.

  3. 3 The documentation for the String class says that it is in the java.lang package. Which of the following is true? 1.7.A.1
    Show answer

    Answer: B. A class defines a specific reference type, and classes in libraries are grouped into packages. A is wrong because being in a package does not make a class primitive; only int, double and boolean are primitive. C is wrong because packages group classes. D is wrong because existing library classes can be used to create objects.

  4. Thermostat documentation
    EntryDescription
    double targetTempThe temperature to reach
    boolean heaterOnWhether the heater is running
    void raise(double amount)Raises the target temperature by amount
    double getTarget()Returns the target temperature
    4 The documentation for a Thermostat class lists the entries shown. Which of the following lists ALL the behaviors of the class? 1.7.A.2
    Show answer

    Answer: C. Behaviors are defined by methods: raise and getTarget (each name is followed by a parameter list). A lists the attributes, which are data stored in variables. B mixes attributes with behaviors. D includes heaterOn, which is an attribute, and misses getTarget.

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.

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.

  • Application programming interface (API)tap to flip

    An API specification is documentation that tells programmers how to use the classes in a library: their attributes and behaviors.

    Example and CED codes
  • Librarytap to flip

    A collection of classes that programmers can use instead of writing everything themselves.

    Example and CED codes
  • Packagetap to flip

    A group of related classes in an API or library, such as java.lang, which holds String and Math.

    Example and CED codes
  • Classtap to flip

    The formal implementation, or blueprint, of the attributes and behaviors of an object. A class defines a reference type.

    Example and CED codes
  • Attributetap to flip

    Data related to a class, stored in variables, such as a plant's name and height.

    Example and CED codes
  • Behaviortap to flip

    What instances of a class can do, or what can be done with them, defined by methods, such as grow or getHeight.

    Example and CED codes
  • Methodtap to flip

    A named block of code that only runs when it is called.

    Example and CED codes
  • Reference typetap to flip

    A data type used to define objects, that is, anything that is not a primitive type. Every class defines a reference type.

    Example and CED codes

Questions students ask

What is an API in Java?

API stands for application programming interface. An API specification is documentation that tells programmers how to use the classes in a library, including their attributes and behaviors.

What is the difference between a library and a package?

A library is a collection of classes. Inside APIs and libraries, related classes are grouped into packages, such as the java.lang package that holds String and Math.

What is the difference between an attribute and a behavior?

An attribute is data related to a class and is stored in a variable, such as a plant's height. A behavior is something an instance can do, or that can be done with it, and is defined by a method, such as grow.

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