APIs and Libraries · API和库
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| library/ˈlaɪbrəri/ | 库 | kù |
| API/ˌeɪ piː ˈaɪ/ | 应用程序接口 | 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.
站在别人的代码之上
- 你不用从头写所有东西——Java 自带大量现成的代码。
- 库是一批你可以使用的、预先写好的类。
- Java 的标准库包括
String、Math、Scanner、ArrayList以及成千上万个。 - 复用经过测试的库代码,比重新发明更快、更安全。
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."
API 告诉你什么
- API(应用程序接口)是一个类所提供内容的文档化清单。
- 它告诉你每个方法的名字、它接受哪些参数、以及它返回什么。
- 你面向 API 编程——你按方法文档化的行为使用它,而非它内部的代码。
- API 是一份合约:“给我这些输入,我就给你这个输出。”
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.
导入一个类
- 有些类在使用前需要一个
import语句。 import java.util.Scanner;让Scanner可用。- 像
String和Math这样的核心类在java.lang里——自动导入。 - import 放在文件顶部,类的上方。
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.
无需看内部就能使用
- 你可以调用
Math.sqrt(x)而不知道它如何计算平方根。 - 这就是抽象:API 把实现藏在一个简单的接口之后。
- 你只需要是什么(文档化的行为),而不需要怎么做。
- 好的库让你把复杂代码当作黑盒,从而快速构建。
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.
你面向 API 文档化的行为编程,而非它内部的代码。读一个方法接受什么、返回什么——你不需要(也不想)看它内部如何工作。记住大多数类需要 import,但 java.lang 里的类(String、Math、System、Integer)会自动导入,所以 Scanner 需要 import java.util.Scanner;,而 Math 不需要。
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."
使用 Math 库:
- 不需要 import——
Math在java.lang里。 double r = Math.sqrt(16);→r是4.0,而你从不看到那个算法。- API 只是承诺了:“给我一个数,我返回它的平方根。”
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.
库是可复用的预写代码;API 记录每个类的方法——它们的名字、参数和返回值——所以你面向文档化的行为编程。大多数类需要 import(如 Scanner),但 java.lang 的类(String、Math)会自动导入。API 就是抽象:无需看内部就能使用。
An API documents a class's methods by telling you their... · API通过说明方法的...来记录类的功能
You program to the documented name/params/return, not the inner code. · 你根据文档化的名称/参数/返回值进行编程,而不是内部代码。
Which class needs an import statement before use? · 哪个类在使用前需要import语句?
java.lang classes import automatically; Scanner (java.util) needs an import. · java.lang类自动导入;Scanner (java.util)需要导入。
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. · Math和String自动导入是因为它们在java.lang中。
java.lang is imported by default in every Java program. · java.lang在每个Java程序中默认导入。