APIs and
Libraries
Learn how Application Program Interfaces let your code communicate with pre-built libraries: the foundation of every real-world Java program.
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.
Why Do APIs Matter?
Anatomy of an API Entry
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.
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
One public counting interface, two hidden mechanisms
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
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
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.
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.
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.
Static vs Instance Methods
No object needed.
Example: Math.abs(-5)
Keyword static in signature.
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.
Common Mistakes
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: