APIs and libraries are like having a massive toolbox where someone else already built all the tools you need. Instead of writing code to do complex math calculations from scratch, you can use the Math class. Instead of building your own data structures, Java provides ArrayList. Learning to use APIs effectively is what separates beginners from programmers who can build real applications.
The key insight about APIs is that you don't need to know how they work internally - you just need to know what they do and how to use them. It's like driving a car without being a mechanic. The API documentation tells you everything you need: what methods are available, what parameters they take, and what they return.
Java organizes its built-in classes into packages, with java.lang being so fundamental it's automatically imported. Understanding how to read API documentation is honestly more important than memorizing specific methods. Once you can navigate the docs, you have access to thousands of pre-built solutions.
- Major concepts: API as a contract, libraries as collections of classes, packages for organization, API documentation structure
- Why this matters for AP: Foundation for using Java's built-in classes, critical for understanding provided classes in FRQs
- Common pitfalls: Not reading return types carefully, forgetting what package a class is in, misunderstanding static vs instance methods in APIs
- Key vocabulary: API (Application Programming Interface), library, package, documentation, class specification
- Prereqs: Basic understanding of classes and methods, difference between static and instance methods
Key Concepts

APIs Define How to Use Classes
An API (Application Programming Interface) is basically a user manual for code. It tells you what classes are available, what methods they have, and how to use them correctly. The "interface" part means it shows you what you can do without revealing how it's done internally.
Think of an API like a restaurant menu. The menu tells you what dishes are available and describes them, but it doesn't give you the recipes. Similarly, API documentation tells you what methods are available and what they do, but not their internal code.
The Java API documentation is your most important reference. It lists every class, every method, what parameters they need, and what they return. Learning to read this documentation effectively is a crucial programming skill.
Libraries Are Collections of Related Classes
A library is a group of classes that work together to provide functionality. The Java Standard Library includes thousands of classes for everything from basic math to network programming. For AP CSA, you'll mainly use classes from a few core packages.
Libraries save you from reinventing the wheel. Why write your own sorting algorithm when Java provides efficient ones? Why create your own random number generator when the Random class exists? Libraries represent years of work by expert programmers.
The power of libraries is that they're tested and optimized. When you use ArrayList from the Java library, you're using code that's been refined over decades and used by millions of programs.
Packages Organize Classes Logically
Packages are like folders that group related classes together. The package name tells you what kind of functionality to expect. For example, java.util contains utility classes like ArrayList and Random, while java.io handles input/output operations.
The most important package is java.lang, which contains fundamental classes like String, Math, and Object. This package is so essential that Java automatically imports it into every program. That's why you can use String without writing an import statement.
Package names follow a naming convention using dots to show hierarchy. It's like a folder structure: java.util.ArrayList means the ArrayList class is in the util folder inside the java folder.
Reading API Documentation
API documentation follows a standard format that tells you everything you need to know about a class. At the top, you'll see the class declaration and a description of what the class does. Then comes the really useful stuff: constructor summaries and method summaries.
For each method, the documentation shows: the method signature (return type, name, parameters), a description of what it does, details about parameters, what it returns, and any exceptions it might throw. This is all the information you need to use the method correctly.
The documentation also shows whether methods are static or instance methods. Static methods are called on the class itself (like Math.random()), while instance methods are called on objects (like str.length()).
Code Examples
Let's explore how to use API documentation and library classes:
// Example: Using the Math class from java.lang public class MathAPIDemo { public static void main(String[] args) { // Math class provides static methods // No need to create an object // Basic math operations double squareRoot = Math.sqrt(16); // Returns 4.0 double power = Math.pow(2, 10); // Returns 1024.0 double absoluteValue = Math.abs(-42); // Returns 42.0 // Rounding methods double ceiling = Math.ceil(4.3); // Returns 5.0 double floor = Math.floor(4.9); // Returns 4.0 long rounded = Math.round(4.6); // Returns 5 // Trigonometry (angles in radians) double sine = Math.sin(Math.PI / 2); // Returns 1.0 double cosine = Math.cos(Math.PI); // Returns -1.0 // Random number between 0.0 and 1.0 double random = Math.random(); // Constants provided by Math class System.out.println("Pi = " + Math.PI); System.out.println("E = " + Math.E); } }
Understanding package imports:
// Example: Different ways to import and use classes // Option 1: Import specific class import java.util.ArrayList; import java.util.Random; // Option 2: Import entire package (not recommended) // import java.util.; public class ImportDemo { public static void main(String[] args) { // ArrayList is from java.util package ArrayList<String> list = new ArrayList<String>(); list.add("Hello"); // Random is also from java.util Random rand = new Random(); int randomNum = rand.nextInt(100); // 0-99 // String is from java.lang - no import needed! String message = "Java APIs are helpful"; // Math is also from java.lang double result = Math.sqrt(25); // Without import, you'd need the full name: // java.util.Scanner scanner = new java.util.Scanner(System.in); } }
Reading and using API documentation patterns:
// Example: Using String class methods based on API docs public class StringAPIExample { public static void main(String[] args) { String text = "Hello World"; // Method: int length() // Returns: the length of this string int len = text.length(); // Returns 11 // Method: String substring(int beginIndex, int endIndex) // Parameters: beginIndex - the beginning index, inclusive // endIndex - the ending index, exclusive // Returns: the specified substring String sub = text.substring(0, 5); // Returns "Hello" // Method: int indexOf(String str) // Parameters: str - the substring to search for // Returns: the index of first occurrence, or -1 if not found int index = text.indexOf("World"); // Returns 6 // Method: String toLowerCase() // Returns: String converted to lowercase String lower = text.toLowerCase(); // Returns "hello world" // Method: boolean equals(Object anObject) // Parameters: anObject - the object to compare // Returns: true if strings are equal boolean same = text.equals("Hello World"); // Returns true } }
Common Errors and Debugging
Forgetting to Import Classes
Classes from packages other than java.lang need to be imported:
// ERROR: Cannot find symbol ArrayList public class NoImport { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); // Error! } } // CORRECT: Import the class import java.util.ArrayList; public class WithImport { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); // Works! } }
Remember that java.lang classes (String, Math, Object) don't need imports. Everything else does.
Confusing Static and Instance Methods
API documentation clearly shows whether a method is static or not:
// Math methods are static - call on the class double result1 = Math.sqrt(16); // Correct Math mathObj = new Math(); // Error - can't instantiate Math double result2 = mathObj.sqrt(16); // Error - not how static methods work // String methods are instance methods - call on objects String str = "Hello"; int len = str.length(); // Correct int badLen = String.length(); // Error - not a static method
Always check the API docs to see if a method is static (has the static keyword) or instance method.
Misreading Method Signatures
Pay attention to parameter types and return types in the documentation:
// The substring method: String substring(int beginIndex, int endIndex) String text = "Hello"; String sub1 = text.substring(1, 3); // Correct: returns "el" String sub2 = text.substring("H"); // Error: parameter must be int, not String int sub3 = text.substring(1, 3); // Error: returns String, not int // The indexOf method: int indexOf(String str) int pos1 = text.indexOf("e"); // Correct: returns 1 String pos2 = text.indexOf("e"); // Error: returns int, not String
Practice Problems
Problem 1: Use the Math class to calculate the hypotenuse of a right triangle:
public static double hypotenuse(double a, double b) { // Calculate c where c² = a² + b² // Your code here }
Solution:
public static double hypotenuse(double a, double b) { // Using Math.pow and Math.sqrt from the API double cSquared = Math.pow(a, 2) + Math.pow(b, 2); return Math.sqrt(cSquared); // Or more concisely: // return Math.sqrt(a * a + b * b); }
Problem 2: What import statement is needed for this code to compile?
public class Mystery { public static void main(String[] args) { Random generator = new Random(); int number = generator.nextInt(10); System.out.println(number); } }
Solution: import java.util.Random;
The Random class is in the java.util package, so it needs to be imported.
Problem 3: Based on typical API documentation, what would this code print?
String word = "COMPUTER"; System.out.println(word.substring(3, 6)); System.out.println(word.indexOf("PUT")); System.out.println(word.length());
Solution:
word.substring(3, 6)
returns "PUT" (characters at indices 3, 4, 5)word.indexOf("PUT")
returns 3 (first occurrence starts at index 3)word.length()
returns 8 (number of characters)
AP Exam Connections
Understanding APIs is crucial for the AP exam because many questions provide custom classes with their documentation. You'll see something like "Consider the following class" followed by method signatures and descriptions. Your ability to use these provided APIs correctly often determines your success.
For multiple choice questions, you'll often trace through code that uses Java's standard library classes. Know the common methods for String, Math, Integer, and Double classes. The exam won't test obscure methods, but you need to understand method signatures and return types.
In FRQs:
- FRQ 1 (Methods/Control): Often uses Math class methods
- FRQ 2 (Class Design): You might need to use provided classes/interfaces
- FRQ 3 (Array/ArrayList): ArrayList is itself a library class
- FRQ 4 (2D Array): May involve using provided utility methods
Key tip: When the exam provides a class with its API, read it carefully! Note whether methods are static or instance methods, what parameters they take, and what they return. Half the battle is just using the provided API correctly.
Remember that you won't have access to Java documentation during the exam, but you also won't need to memorize every method. The exam will either use very common methods (like String's length() or substring()) or provide the documentation you need.
Frequently Asked Questions
How do I find out what methods are available in the `String` class?
Look up the String class in the Java API (Javadoc) or the AP Java Quick Reference. The official Javadoc for java.lang.String lists every public constructor, field, and method (behaviors) with signatures and descriptions—search for "java.lang.String Javadoc" online or open it from your IDE. The AP Java Quick Reference (provided on the exam appendix) also lists commonly accessible String methods you need for AP CSA: length(), substring(from), substring(from,to), indexOf(...), equals(...), compareTo(...), split(...), etc. Quick practical ways: - In an IDE (IntelliJ, Eclipse, VS Code) type "String." and use autocomplete to see available methods and signatures. - Read the Javadoc to understand return types, parameters, and behavior (important for exam correctness). - Review the AP Topic 1.7 study guide on Fiveable for examples and common String methods (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N). For broader review, check Unit 1 (https://library.fiveable.me/ap-computer-science-a/unit-1) and practice problems (https://library.fiveable.me/practice/ap-computer-science-a).
What's the difference between an API and a library in Java?
Short answer: a library is the actual collection of code (classes, constructors, fields, methods) you can use; an API is the specification/documentation that tells you how to use that library. More detail: in Java the Standard Library (e.g., java.util.ArrayList, java.util.HashMap) is a library—packaged sets of classes grouped into packages. The API (Application Programming Interface) is the written contract/Javadoc that describes a class’s attributes (fields) and behaviors (methods), access modifiers, constructors, return types, and how you should call them (EK 1.7.A.1, EK 1.7.A.2). The API does not contain the source you write against; it tells you what methods exist, their parameters, and what they do. On the AP exam you’ll be asked to use library classes and read API-style specs (the Java Quick Reference is provided during the exam), so being comfortable reading Javadoc and package/class docs is essential. Want practice? Check the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and try problems on the AP practice page (https://library.fiveable.me/practice/ap-computer-science-a).
I'm confused about what attributes and behaviors mean - can someone explain with examples?
Think of a class like a real-world object: attributes = what it “has” (data), behaviors = what it “does” (actions). In Java terms (CED EK1.7.A.2): attributes are fields (instance variables) and behaviors are methods. Examples: - java.util.ArrayList: attributes (internal size field, the backing array) store data; behaviors include add(E), get(int), remove(int), size()—those are methods you call to manipulate the object. - A custom Date class: attributes might be private int day, private String month; behaviors are getDay(), setDay(int), toString(). - Immutable class example: java.lang.String has attributes (the characters) but no mutator methods that change them; behaviors return new Strings (substring, concat). On the AP exam you’ll read API docs/Javadoc to identify fields and methods (LO 1.7.A). Practice spotting fields vs. methods in the Java Quick Reference and library classes—see the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and drill with Fiveable practice questions (https://library.fiveable.me/practice/ap-computer-science-a).
How do I know which package a class belongs to when I'm coding?
Most of the time you find a class’s package by checking the API docs (Javadoc) or your IDE’s autocomplete. Important tips tied to the AP CED: - Common built-ins: classes in java.lang (String, Math, Object) are automatically available—no import needed. - For library classes you’ll usually see fully qualified names like java.util.ArrayList or java.util.HashMap in the docs; use that package in an import (import java.util.ArrayList;). - Use your IDE: it suggests imports or shows the package when you hover the class. - If you don’t want imports, use the fully qualified name in code (java.util.ArrayList
What's the syntax for importing a library in Java?
In Java you bring library classes into your file with an import statement placed after any package declaration and before your class. Common forms: - Single class: import java.util.ArrayList;
- Whole package: import java.util.*;
- Static members: import static java.lang.Math.PI; or import static java.lang.Math.*; You can also use a class’s fully qualified name without import (e.g., java.util.ArrayList
When should I use existing classes vs creating my own classes?
Use existing library classes when they already model the data or behavior you need and are well-documented in the API (java.lang, java.util, etc.). The CED’s EK 1.7.A.1 says libraries are collections of classes—so prefer reuse for common needs (Strings, ArrayList, HashMap, File I/O, math). Check the API/Javadoc to confirm constructors, fields, and accessible methods before using them. Create your own class when your problem needs specific attributes or behaviors not provided (domain logic, invariants, or custom state), when you need encapsulation (private fields + getters/setters), or when you must control mutability, constructors, or overloaded methods. If you find yourself wrapping a library class just to add a little logic often, design a custom class instead. On the AP exam, you’ll be expected to read and use library methods from the Java Quick Reference and identify class attributes/behaviors (LO 1.7.A). For focused review, check the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and practice problems (https://library.fiveable.me/practice/ap-computer-science-a).
Why do I need to look at documentation when using API classes?
You need documentation because an API class’s behavior isn’t obvious just from its name—the docs tell you the class’s constructors, fields, methods, return types, parameter types, access modifiers, and important details like preconditions, side effects, and whether the class is immutable. Without that you might call the wrong constructor, expect a method to change an object when it returns a new one, or misuse an overloaded method. On the AP exam you get a Java Quick Reference with some library methods, but real projects and many exam-style problems require checking the full Javadoc (e.g., how ArrayList.get vs. remove behave, or whether a method can return null). Reading docs prevents bugs, saves time, and helps you use existing libraries correctly—a core CED skill (identify attributes/behaviors of API classes). For a quick recap, see the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and practice problems (https://library.fiveable.me/practice/ap-computer-science-a).
I don't understand how to read Java API documentation - it's so confusing!
Start with what the API doc is telling you: attributes = fields (data) and behaviors = methods (what you can do). Scan the class page top-to-bottom: - Package + class description: what the type represents. - Constructors: how to create instances (parameters, order). - Fields: instance data you can access (note access modifiers—private vs public). - Methods table: read each signature (return type, name, parameter list)—that tells you behavior, inputs, and outputs. - Method detail: look for Javadoc notes about side effects, return conditions (null?), thrown exceptions, and examples. Overloaded methods share a name but different params—match the one you need. - Pay attention to access modifiers (public methods are usable on the exam), immutability remarks, and any “precondition/postcondition” language (CED-style). - For exam practice, use the Java Quick Reference and focus on commonly tested classes (ArrayList, String, Math). If you want guided walkthroughs and practice reading docs, check the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). For extra practice, Fiveable’s problem sets are useful (https://library.fiveable.me/practice/ap-computer-science-a).
What's the difference between instance variables and methods in a class?
Instance variables (also called fields or attributes) store the data for each object of a class—the “state.” Example: private int score; private String name;. They’re defined in the class and each object has its own copy (unless marked static). On the AP CED this maps to “attributes” and “fields” (EK 1.7.A.2). Methods define the class’s behaviors—what you can do with an object. They use or change instance variables, return values, and can be constructors, getters, setters, or other actions (mutators). Example: public int getScore() { return score; } or public void setScore(int s) { score = s; }. Key AP points: read the API docs to see a class’s fields and accessible methods (LO 1.7.A). Remember access modifiers: private fields + public getters/setters is common; immutable classes omit mutators. You’ll need to declare instance variables, a constructor, and at least one method on AP free-response Question 2 (Class Design). For a quick refresher, see the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N)—Fiveable’s guide and the Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1) are handy for practice.
How do I write code that uses the `ArrayList` class from the Java library?
ArrayList is in java.util and is a resizable list class you’ll use a lot on the AP exam (see Topic 1.7). Import it, pick a type with generics, construct it, then call methods like add, get, set, remove, and size. Example:
import java.util.ArrayList;
ArrayList
Can someone explain what a package is and why classes are grouped into them?
A package is a named group (folder) of related classes in a library or API. In Java the API groups classes into packages like java.util (ArrayList, HashMap) and java.lang (String, Object). Packages help by: 1) organizing code so you can find related classes and their Javadoc in the API; 2) providing a namespace so two classes with the same name (e.g., util.Date vs. sql.Date) don’t conflict; 3) controlling access (package-private visibility) and making reuse easier; and 4) letting you import only what you need (import java.util.ArrayList). For the AP exam you should be able to identify class attributes and methods from the Java libraries and read API docs (EK 1.7.A.1–A.2). Want a quick refresher and examples? See the AP Topic 1.7 study guide on Fiveable (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and more Unit 1 review (https://library.fiveable.me/ap-computer-science-a/unit-1).
I'm getting an error that says the class cannot be resolved - how do I fix this?
“Class cannot be resolved” means the compiler can’t find the class you’re trying to use. Common fixes (check these in order): - Spelling & case: Java is case-sensitive. Make sure the class name matches exactly (including caps). - File name / public class: If a class is public, the filename must match (MyClass -> MyClass.java). - Package and imports: If the class is in a package (e.g., java.util.ArrayList or com.myapp.Utils), either import it or use the fully qualified name. Add import java.util.ArrayList; for library classes not in java.lang. - Classpath / dependencies: If the class is in an external JAR or library, add that JAR to your project/classpath or your build tool (IDE project settings, javac -cp, or build.gradle/maven). - Compile order: If you wrote multiple classes, compile them (or let the IDE) so dependent classes exist. In command line, javac *.java or compile the needed files. - Access modifier: Ensure the class is public (or package-private but in the same package). - IDE quick-fixes: Most IDEs offer “import class” or add library to project—use those. On the AP exam you’ll only use classes from the Java Quick Reference (and they’re implicitly available), but in labs/projects you must manage imports and classpath. For a quick refresher on API, packages, and imports, see the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N). For extra practice problems, check (https://library.fiveable.me/practice/ap-computer-science-a).
What are some common Java API classes that I should know for the AP exam?
You should know the standard library classes you’ll actually see on the exam and how to read their API (constructors, fields, methods, access levels). Key ones to focus on: - String (immutable): length(), substring(), indexOf(), equals(), compareTo()—used a lot in FRQs. - Math: abs(), pow(), sqrt(), random()—common utility methods. - Wrapper classes: Integer, Double (parseInt, parseDouble, MIN/MAX). - java.util.ArrayList: size(), add(), get(), set(), remove()—FRQ 3 and many prompts use ArrayList. - java.util.HashMap: put(), get(), containsKey(), keySet()—useful for mapping tasks. - java.lang.Object: equals(), toString() (know inheritance basics). - Arrays utilities (in Quick Reference): be familiar with primitives & 2D array indexing. Remember the AP exam gives a Java Quick Reference (so know which methods are listed). Practice reading Javadoc-style specs: identify constructors, fields, method signatures, return types, and pre/postconditions (EK 1.7.A.1–A.2). For a focused review, see the AP Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and Unit 1 overview (https://library.fiveable.me/ap-computer-science-a/unit-1). For lots of practice problems, try the Fiveable practice set (https://library.fiveable.me/practice/ap-computer-science-a).
How do I trace through code that uses library methods I've never seen before?
Focus on the method signature and what the API documentation says—those two things let you trace code even if you’ve never seen the method before. Steps to trace: 1. Read the method header: parameter types, return type, and access (constructor? static?). That tells you what data it needs and what it produces (EK 1.7.A.1, EK 1.7.A.2). 2. Check the Java Quick Reference / Javadoc for the class (what package, whether method mutates the object or returns a new one). Immutable vs mutable matters for state changes. 3. If docs aren’t available in the problem, use the method name and types to infer behavior (e.g., add/get/set on ArrayList modifies vs returns). Note overloaded versions by parameter list. 4. Simulate with concrete values step-by-step: track variables, object references, return values. Use a small table or write a quick main() that calls the method to see results. 5. Use print statements or a debugger to watch object fields, especially after calls that might mutate state (constructors, setters). 6. When on the AP exam, rely on the Java Quick Reference and described accessible methods—don’t assume undocumented side effects. For more on APIs and practice tracing examples, see the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N) and try practice problems (https://library.fiveable.me/practice/ap-computer-science-a).
When do I use `import` statements and when don't I need them?
Use import when you want to refer to classes by their simple names instead of full package names. Key rules: - You need import when using a library class from a package other than java.lang (e.g., java.util.ArrayList). Add: import java.util.ArrayList; then use ArrayList in code. - You don’t need import for classes in java.lang (String, Math)—they’re automatically available. - You don’t need import if you use the class’s fully-qualified name everywhere (e.g., java.util.HashMap m = new java.util.HashMap();). - You don’t need import for classes in the same package as your class. - import doesn’t “load” code—it just lets the compiler recognize the short name. Wildcard imports (import java.util.*;) are allowed but the AP style favors explicit imports for clarity. On the AP exam you get a Java Quick Reference (see CED Appendix) so you’ll often rely on listed classes; for study, check the Topic 1.7 study guide (https://library.fiveable.me/ap-computer-science-a/application-program-interface-api-and-libraries/study-guide/sEpH1rg9TzREJwynSE9N). For more practice, use the unit overview (https://library.fiveable.me/ap-computer-science-a/unit-1) and practice problems (https://library.fiveable.me/practice/ap-computer-science-a).