01 / 20

APIs and
Libraries

Learn how Application Program Interfaces let your code communicate with pre-built libraries: the foundation of every real-world Java program.

AP Computer Science A

What Is an API?

An Application Program Interface (API) is a set of specifications that describe what a class or library can do: its public methods, constructors, and return types: without revealing how it does it internally.

What Is a Library?

A library is a collection of pre-written classes and methods that you can import and reuse in your own programs. Java ships with a massive standard library (the Java API), and you can also add third-party libraries.

java.lang
Automatically imported. Includes String, Math, Integer, and System.
java.util
Must be imported. Includes ArrayList, Scanner, and Random.
java.io
Must be imported. Provides classes for file input and output operations.

Why Do APIs Matter?

1
Abstraction: You use methods without knowing the internal implementation details.
2
Code Reuse: Thousands of tested methods are ready to use: no need to reinvent the wheel.
3
Collaboration: Teams agree on method signatures (the API contract), then build independently.
4
Reliability: Library code is used by millions and is thoroughly tested and optimized.

Anatomy of an API Entry

Method Signature from the Java API Documentation RETURN TYPE double METHOD NAME sqrt PARAMETER(S) (double a) MODIFIER static Full signature: public static double sqrt(double a) Returns the correctly rounded positive square root of a double value. Found in: java.lang.Math

The import Statement

To use a class from a library outside of java.lang, you need an import statement at the top of your file. This tells the compiler where to find the class definition.

// Import a single class import java.util.ArrayList; // Import all classes in a package import java.util.*;

API vs Library vs Package

API Library Package
What is it? A specification (the "contract") A collection of compiled code A folder that organizes classes
Contains Method signatures, descriptions Classes and their implementations Related classes grouped by name
Example Java API docs Java Standard Library java.util
Analogy A restaurant menu The entire kitchen A section of the menu

How the Pieces Fit Together

Your Code MyProgram.java calls API (Contract) Math.sqrt(double) String.length() ArrayList.add(E) Scanner.nextInt() method signatures maps to Library (Implementation) java.lang java.util compiled .class files You interact with the API. The Library does the work behind the scenes.

One public counting interface, two hidden mechanisms

Mechanical and electronic counting instruments have matching front controls and different interiors.Two implementations can provide the same public counting behavior while storing their internal state differently. A representation change must preserve the interface’s promised behavior.

Preserve public behavior across implementations

Two Counter implementations trace public outputs 0, 1, 2 and 3 while storing opposite internal values.Starting at zero, one Counter stores count and increments it; another stores negativeCount and decrements it, returning its negation. Three increment calls make both getCount results 3. Their private representations differ while the public trace agrees.

How to Read API Documentation

1
Find the class name and the package it belongs to (e.g., java.util.ArrayList).
2
Check the constructor(s): How do you create an object of this class?
3
Scan the method summary: Note the return type, method name, and parameters.
4
Read the description: Understand what the method does, any preconditions, and edge cases.
5
Check if it's static: Static methods are called on the class; instance methods are called on an object.

Example 1: Math Class (Static Methods)

The Math class is in java.lang, so no import is needed. All its methods are static: you call them on the class itself, not on an object.

// No import needed — java.lang is automatic double result = Math.sqrt(144); // 12.0 int bigger = Math.max(7, 15); // 15 double power = Math.pow(2, 10); // 1024.0 int absolute = Math.abs(-42); // 42

Example 2: ArrayList (Instance Methods)

ArrayList is in java.util, so you must import it. Its methods are instance methods: you call them on an object you create.

import java.util.ArrayList; ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); // adds to end names.add("Bob"); // adds to end names.add(1, "Carol"); // inserts at index 1 String first = names.get(0); // "Alice" int size = names.size(); // 3

Example 3: The String API

The String class has a rich API with many useful instance methods. Let's explore a few from the AP exam subset.

String msg = "Hello, World!"; msg.length() // 13 msg.substring(0, 5) // "Hello" msg.indexOf("World") // 7 msg.equals("Hello, World!") // true msg.compareTo("Apple") // positive (H > A)

Static vs Instance Methods

Static Methods
Called on the class itself.
No object needed.
Example: Math.abs(-5)
Keyword static in signature.
Instance Methods
Called on an object.
Must use new first.
Example: str.length()
No static keyword.

Overloaded Methods in APIs

Many API methods are overloaded: same name, different parameter lists. The compiler picks the correct version based on the arguments you pass.

// ArrayList.add is overloaded: names.add("Dave"); // add(E e) — appends to end names.add(0, "Dave"); // add(int index, E e) — inserts // Math.abs is overloaded: Math.abs(-5); // abs(int a) → int Math.abs(-3.14); // abs(double a) → double

Common Mistakes

✕ Forgetting to Import
Using ArrayList or Scanner without import java.util.*; causes a compile error.
✕ Calling Static on Object
Writing myMath.sqrt(4) instead of Math.sqrt(4). Static methods belong to the class.
✕ Wrong Parameter Types
Passing a String to Math.abs() or an int where an object is needed. Check the API signature.
✕ Ignoring Return Values
Calling str.toUpperCase() without storing the result. Strings are immutable: the original doesn't change.

Question 1 of 5

Which of the following best describes an API?

Question 2 of 5

What import statement is needed to use the Math class in Java?

Question 3 of 5

Consider the following code segment:

String s = "computer";
String t = s.substring(3, 6);


What is the value of t?

Question 4 of 5

Which of the following correctly calls the static method max from the Math class?

Question 5 of 5

Two methods in a class have the same name but different parameter lists. This is an example of: