Topic 1.7 — Application Program Interface (API) and Libraries
Goal: understand what an API is, how libraries provide prewritten classes and methods, and how to use documentation to call those methods correctly.
The big idea
You don’t have to write everything from scratch. Java includes many prewritten classes that you can use. An API (Application Programming Interface) is basically the “menu” of what those classes and methods can do.
In CSA, you learn to use library code correctly by reading documentation and calling methods with the right name and parameters.
Key vocabulary
- Library: a collection of prewritten code you can reuse.
- API: the set of available classes, methods, and rules for using them.
- Class: a blueprint for objects (and also where methods can live).
- Method: a named action you can call.
- Documentation: explains how to use classes/methods (names, parameters, return types, behavior).
Why APIs are helpful
- Saves time (someone already solved common problems).
- Makes programs more reliable (library code is tested).
- Makes your code shorter and easier to read.
CSA mindset
You are responsible for knowing what the method call does and what value it returns (if any), even if you didn’t write the method.
Reading documentation (what to look for)
When you look at a method in documentation, you should identify these parts:
1) Method name
The exact spelling matters.
abs
pow
substring2) Parameters
Inputs in parentheses; order matters.
pow(double a, double b)
substring(int beginIndex, int endIndex)3) Return type
What you get back (or void if nothing).
int length()
double random()
void println(String x)4) Description / behavior
Rules, edge cases, and what the method guarantees.
Library method call pattern
A common pattern you’ll see:
ClassName.methodName(arguments);
This kind of call is used a lot for class methods (you’ll formalize that in Topic 1.10).
Example: using a library class (preview)
Here’s what it looks like to call a method from a Java library class:
double r = Math.random();
You don’t need to know all details yet—just recognize “call a method from a library.”
Common mistakes
- Wrong method name (spelling/case) → compile-time error.
- Wrong number of arguments → compile-time error.
- Wrong argument types → compile-time error.
- Ignoring return values (when you needed them) → logic error.
How to be “API-smart”
- Check the method name exactly.
- Match parameter types and order.
- Look at the return type and store it if needed.
- Use the documentation’s description to predict behavior.
Quick self-check
- In one sentence, what is an API?
- What is the difference between a library and an API?
- Why is the return type important?
- Name two things you always check in documentation before calling a method.
- What type of error happens if you call a method with the wrong number of arguments?