APIs and Libraries
| English | Chinese | Pinyin |
|---|---|---|
| library | 库 | kù |
| API | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
Standing on others' code
- You don't write everything from scratch — Java ships with huge amounts of ready-made code.
- A library 库 is a collection of pre-written classes you can use.
- Java's standard library includes
String,Math,Scanner,ArrayList, and thousands more. - Reusing tested library code is faster and safer than reinventing it.
What an API tells you
- An API (Application Program Interface) 应用程序接口 is the documented list of what a class offers.
- It tells you each method's name, what parameters it takes, and what it returns.
- You program to the API — you use a method by its documented behavior, not its inner code.
- The API is a contract: "give me these inputs, I'll give you this output."
Importing a class
- Some classes need an
importstatement before you use them. import java.util.Scanner;makesScanneravailable.- Core classes like
StringandMathare injava.lang— imported automatically. - The import goes at the top of the file, above the class.
Use it without seeing inside
- You can call
Math.sqrt(x)without knowing how it computes a square root. - This is abstraction: the API hides the implementation behind a simple interface.
- You only need the what (the documented behavior), not the how.
- Good libraries let you build fast by treating complex code as a black box.
Use a library, or write it yourself?
Sort each task by whether a standard library already does it.
You program to the API's documented behavior, not its internal code. Read what a method takes and returns — you don't need (or want) to see how it works inside. Remember most classes need an import, but java.lang classes (String, Math, System, Integer) are imported automatically, so Scanner needs import java.util.Scanner; while Math does not.
Using the Math library:
- No import needed —
Mathis injava.lang. double r = Math.sqrt(16);→ris4.0, and you never see the algorithm.- The API just promised: "give me a number, I return its square root."
A library is reusable pre-written code; the API documents each class's methods — their names, parameters, and return values — so you program to the documented behavior. Most classes need an import (e.g. Scanner), but java.lang classes (String, Math) are imported automatically. The API is abstraction: use it without seeing inside.
An API documents a class's methods by telling you their...
You program to the documented name/params/return, not the inner code.
Which class needs an import statement before use?
java.lang classes import automatically; Scanner (java.util) needs an import.
You must read a library method's internal code before you can use it.
You use it by its documented behavior — that's abstraction.
A collection of pre-written, reusable classes is called a ___ (one word).
A library bundles reusable code.
Math and String are imported automatically because they are in java.lang.
java.lang is imported by default in every Java program.