APIs và Thư viện
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| library/ˈlaɪbrəri/ | thư viện |
| API/ˌeɪ piː ˈaɪ/ | giao diện lập trình ứng dụng |
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."

A library groups classes; each class documents the methods you can call
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.
Sử dụng thư viện hay tự viết?
Sắp xếp mỗi nhiệm vụ dựa vào việc thư viện tiêu chuẩn đã thực hiện nó chưa.
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.
Một API tài liệu hóa các phương thức của lớp bằng cách cung cấp cho bạn...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Bạn lập trình theo tên/tham số/trả về đã tài liệu hóa, không phải mã bên trong.
Lớp nào cần câu lệnh import trước khi sử dụng?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Các lớp java.lang được import tự động; Scanner (java.util) cần một câu lệnh import.
Bạn phải đọc mã nội bộ của phương thức thư viện trước khi sử dụng nó.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Bạn sử dụng nó thông qua hành vi đã tài liệu hóa — đó chính là sự trừu tượng.
Một tập hợp các lớp được viết sẵn, tái sử dụng được gọi là ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Thư viện đóng gói code có thể tái sử dụng.
Math và String được import tự động vì chúng nằm trong java.lang.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
java.lang được import mặc định trong mọi chương trình Java.