# AP® Computer Science A 1.7 Application Program Interface (API) and Libraries

Source: https://lockincs.com/ap-csa/unit-1/1-7-application-program-interface-api-and-libraries/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.7.A, 1.7.A.1, 1.7.A.2

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.

## What you will learn

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

## 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.

## Common mix-ups

- Not true: You have to write every class you use. 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.
- Not true: A method is an attribute because it belongs to the class. Attributes are data stored in variables (nouns, such as height). Behaviors are actions defined by methods (verbs, such as grow).
- Not true: An API is a program you run. An API specification is documentation. It informs the programmer how to use the classes in a library.

## In 30 seconds

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

## Checkpoint: 4 practice questions

### Question 1

The API documentation for a `Robot` class shows a variable `batteryLevel` and a method `moveForward()`. Which statement is correct?

- A. `batteryLevel` is a behavior and `moveForward` is an attribute.
- B. Both are attributes.
- C. Both are behaviors.
- D. `batteryLevel` is an attribute and `moveForward` is a behavior.

**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.

### Question 2

Which of the following best describes the relationship between a library and an application programming interface (API) specification?

- A. A library holds classes; its API specification documents how to use them.
- B. An API specification is a library that holds only class methods.
- C. A library is one class, and an API is the package that contains it.
- D. Libraries and API specifications are two names for the same package of methods.

**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.

### Question 3

The documentation for the `String` class says that it is in the `java.lang` package. Which of the following is true?

- A. `String` is a primitive type, because it is in `java.lang`.
- B. `String` defines a reference type, grouped with related classes.
- C. Packages contain only methods, so `String` must be a method.
- D. A program cannot create `String` objects without writing the `String` class.

**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.

### Question 4

**Thermostat documentation**

| Entry | Description |
| --- | --- |
| double targetTemp | The temperature to reach |
| boolean heaterOn | Whether the heater is running |
| void raise(double amount) | Raises the target temperature by amount |
| double getTarget() | Returns the target temperature |

The documentation for a `Thermostat` class lists the entries shown. Which of the following lists ALL the behaviors of the class?

- A. `targetTemp` and `heaterOn`
- B. `targetTemp`, `heaterOn`, `raise` and `getTarget`
- C. `raise` and `getTarget`
- D. `heaterOn` and `raise`

**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`.

## 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.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
