1.7 Application Program Interface (API) and Libraries
💻AP Computer Science A Unit 1 Review
1.7 Application Program Interface (API) and Libraries
💻 AP Computer Science A
1.1
Why Programming? Why Java?
1.2
Variables and Primitive Data Types
1.3
Expressions and Assignment Statements
1.4
Assignment Statements and Input
1.5
Casting and Ranges of Variables
1.6
Compound Assignment Operators
1.7
Application Program Interface (API) and Libraries
1.8
Documentation With Comments
1.9
Calling a Void Method With Parameters
1.10
Calling a Non-Void Method
1.11
Using the Math Class
1.12
Objects: Instances of Classes
1.13
Creating and Storing Objects
1.14
Calling a Void Method
1.15
String Methods
TLDR
Libraries are collections of classes, and an API specification is the documentation that tells you how to use those classes without seeing their internal code. In AP Computer Science A, your job is to read that documentation to identify a class's attributes (its data, stored in variables) and its behaviors (what it can do, defined by methods).
An API specification is the documentation that tells you how to use the classes in a library without showing their code. Getting comfortable with attributes, behaviors, packages, and documentation now makes the Math class, the String class, and any provided classes much easier to work with later.
How APIs Describe the Classes in a Library
- A library is a collection of classes, and an API specification explains how to use those classes.
- API documentation is essential for understanding a class's attributes and behaviors, since you use a class without seeing its internal code.
- Attributes are the data related to a class and are stored in variables; behaviors are what instances can do and are defined by methods.
- A class defines a specific reference type, and existing classes can be used to create objects.
- Classes in APIs and libraries are grouped into packages.
Key Concepts
APIs Tell You How to Use a Class
An application programming interface (API) specification is the documentation that informs the programmer how to use the classes in a library. The "interface" part means it shows you what you can do with a class without revealing how it works internally.
Think of an API like a restaurant menu. The menu lists the dishes and describes them, but it does not hand you the recipes. API documentation works the same way: it tells you what methods a class has and what they do, but not the internal code behind them. This is why you can use a class confidently just by reading its documentation.
Libraries Are Collections of Classes
A library is a collection of classes that you can reuse instead of writing everything from scratch. Existing classes and class libraries can be utilized to create objects, which saves you from rebuilding common functionality yourself.
Each class in a library defines a specific reference type. When you create an object from one of these classes, the variable that holds it stores a reference to that object rather than the object itself.
Packages Group Classes Together
Classes in APIs and libraries are grouped into packages. A package is a way of organizing related classes under a shared name. Package names use dots to show hierarchy, similar to nested folders.
One package worth knowing now is java.lang, which contains fundamental classes like String, Math, and Object. Classes in java.lang are available by default, which is why you can use String and Math without any extra setup.
Attributes and Behaviors
Every class describes two things: attributes and behaviors.
- Attributes are the data related to the class. They are stored in variables.
- Behaviors are what instances of the class can do, or what can be done with them. They are defined by methods.
When you read API documentation, you are really looking for these two things. The variables tell you what data an object holds, and the methods tell you what that object can do.
Code Examples
The classes below come from java.lang, so they are available by default. These examples show how reading a class's documented behaviors lets you use it correctly.
</>Java// Using the Math class from java.lang (available by default) public class MathAPIDemo { public static void main(String[] args) { // Math methods are class methods, so call them on the class name double squareRoot = Math.sqrt(16); // returns 4.0 double power = Math.pow(2, 10); // returns 1024.0 double absoluteValue = Math.abs(-42); // returns 42 (an int, widened to 42.0 when stored in the double) // random() returns a double >= 0.0 and < 1.0 double random = Math.random(); System.out.println(squareRoot); System.out.println(power); System.out.println(absoluteValue); System.out.println(random); } }
Reading documented behaviors before calling a method:
</>Java// Using documented String behaviors (String is in java.lang) public class StringAPIExample { public static void main(String[] args) { String text = "Hello World"; // int length() returns the number of characters int len = text.length(); // returns 11 // String substring(int from, int to) returns chars from..to-1 String sub = text.substring(0, 5); // returns "Hello" // int indexOf(String str) returns the first index, or -1 if not found int index = text.indexOf("World"); // returns 6 // boolean equals(Object other) compares character sequences boolean same = text.equals("Hello World"); // returns true System.out.println(len); System.out.println(sub); System.out.println(index); System.out.println(same); } }
Notice that you never see the code inside length(), substring(), or Math.sqrt(). You only need the documented behavior to use them correctly.
Reading Provided Class Documentation and Tracing Calls
Reading a Provided Class: When a question gives you a class, scan its documentation for two things: the attributes (variables that hold data) and the behaviors (methods you can call). You do not need to know how a method is written to use it. You only need to know what it does, what it takes in, and what it returns.
Code Tracing: When you trace code that calls library methods, treat the documented behavior as the source of truth. For a method like substring(int from, int to), the documentation tells you it returns the characters from index from up to but not including to. Trust the description rather than guessing.
Common Trap: Match every method call to what the documentation actually says. Check the parameter types, the order of parameters, and the return type before you assume an output. Many wrong answers come from skipping past these details.
Common Misconceptions
- Misconception: You need to see a class's internal code to use it.Actually: You do not. The API specification gives you the documented attributes and behaviors, which is all you need to use the class.
- Misconception: An API and a library are the same thing.Actually: A library is the collection of classes. The API specification is the documentation that explains how to use those classes.
- Misconception: Every class must be imported.Actually: Classes in
java.lang, such asString,Math, andObject, are available by default and do not need extra setup. - Misconception: Attributes and behaviors are the same kind of thing.Actually: Attributes are data stored in variables, while behaviors are actions defined by methods. Keep the two ideas separate when you read documentation.
- Misconception: Methods on a String change the original String.Actually: They do not. String methods return new values, and the original String object stays the same.
Related AP Computer Science A Guides
Practice Questions for 1.7
1 of 7
Consider the following code segment:
</>Javaint x = 5; int y = 2; double step1 = Math.pow(x, y) / y; int step2 = (int) step1 + (int) 0.8; System.out.print(step2);
What is printed as a result of executing this code segment?
A.
The printed value is 13
B.
The printed value is 12
C.
The printed value is 25
D.
The printed value is 5
Vocabulary
The following words are mentioned explicitly in the AP® course framework for this topic.Term | Definition |
|---|---|
API | An application programming interface that provides a specification informing programmers how to use classes and libraries. |
attribute | The data or properties that define the state of an object or class. |
behavior | The methods or actions that an object or class can perform. |
class | A formal implementation or blueprint that defines the attributes and behaviors of objects. |
library | Collections of classes that can be utilized to create objects and are documented in API specifications. |
method | A named block of code that only runs when it is called, allowing programmers to reuse code and organize programs into logical sections. |
object | A specific instance of a class with defined attributes and behaviors. |
package | Organizational groupings of classes within APIs and libraries. |
reference type | A data type that holds a reference (memory address) to an object rather than storing the object's value directly. |
See Fiveable more often in Google
Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.