| Learning Objective | Essential Knowledge |
|---|---|
1.1.A |
|
1.1.B |
|
1.1.C |
|
使用对象与方法
AP 计算机科学 A · 第 1 主题
1.1
算法、编程与编译器导论
大纲
来源:美国大学理事会 AP 课程与考试说明
一个算法(algorithm)是一个解决一个问题的有限的、逐步的程序。一个程序(program)用一门计算机能运行的语言表达一个算法。Java 是编译(compiled)的:编译器(compiler)把你的源代码翻译成字节码(bytecode),它由 Java 虚拟机(JVM)(Java Virtual Machine)运行。一个语法错误(syntax error)(破坏语法)被编译器捕捉;一个逻辑错误(logic error)(错误的结果)不被——程序运行但行为不当。

| 英文 | 中文 | 拼音 |
|---|---|---|
| algorithm | 算法 | suàn fǎ |
| program | 程序 | chéng xù |
| compiled | 编译 | biān yì |
| compiler | 编译器 | biān yì qì |
| syntax error | 语法错误 | yǔ fǎ cuò wù |
| logic error | 逻辑错误 | luó jí cuò wù |
1.2
变量与数据类型
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.2.A |
|
1.2.B |
|
来源:美国大学理事会 AP 课程与考试说明
一个变量(variable)是一个存储一个固定类型(type)的值的命名的盒子。Java 的主要基本类型(primitive types)是 int(整数)、double(小数)和 boolean(true/false)。类型在先地声明:

int score = 90;
double price = 4.99;
boolean passed = true;
Explore how a variable holds one value at a time
A variable is a named box that stores one value of a fixed type. Step through the lines and watch each box take its value; notice that reassigning score overwrites the old number rather than making a new box.
| 英文 | 中文 | 拼音 |
|---|---|---|
| variable | 变量 | biàn liàng |
| type | 类型 | lèi xíng |
| primitive types | 基本类型 | jī běn lèi xíng |
1.3
表达式与输出
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.3.A |
|
1.3.B |
|
1.3.C |
|
来源:美国大学理事会 AP 课程与考试说明
一个表达式(expression)组合值和运算符(operators)来计算一个结果:+ - * / 和 %(取模(modulus),余数)。整数除法(integer division)截断:7 / 2 是 3,而 7 % 2 是 1。运算符优先级(operator precedence)遵循数学(*、/、% 在 +、- 之前)。用以下打印:
System.out.print("no newline");
System.out.println("with newline");
整数除以整数 0(像 7 / 0)是不允许的,在运行时以一个 ArithmeticException 崩溃。在一个字符串里,一个反斜杠标记一个转义序列(escape sequence):\" 打印一个双引号,\\ 一个单反斜杠,而 \n 开始一个新行——所以 System.out.println("She said \"hi\""); 打印 She said "hi"。
Explore the order of operations step by step
Java applies *, /, % before + and -, working left to right. Watch each step and see why 2 + 3 * 4 is $14$, not $20$ — the multiplication happens first.
| 英文 | 中文 | 拼音 |
|---|---|---|
| expression | 表达式 | biǎo dá shì |
| modulus | 取模 | qǔ mó |
| escape sequence | 转义序列 | zhuǎn yì xù liè |
1.4
赋值语句与输入
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.4.A |
|
1.4.B |
|
来源:美国大学理事会 AP 课程与考试说明
一个赋值(assignment)x = expr; 求右侧的值并把它存进左边的变量。用一个 Scanner 读输入:
Scanner in = new Scanner(System.in);
int age = in.nextInt();
String name = in.next();
| 英文 | 中文 | 拼音 |
|---|---|---|
| assignment | 赋值 | fù zhí |
1.5
类型转换与变量取值范围
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.5.A |
|
1.5.B |
|
1.5.C |
|
来源:美国大学理事会 AP 课程与考试说明
每个类型有一个固定的范围;一个 int 超过约 21 亿溢出。类型转换(casting)在类型之间转换。加宽(int 到 double)是自动的;缩窄需要一个显式的转换,它截断(不舍入):
double avg = (double) total / count; // force real division
int whole = (int) 3.9; // 3, truncated
考试技能: 注意整数除法在期望一个小数时产生一个截断的结果——先把一个操作数转换成 double。
Worked example. 跟踪每个表达式:
7 / 2→3(两个都是int,所以除法截断);7.0 / 2→3.5(一个double迫使实数除法);7 % 2→1(余数);(double) 7 / 2→3.5(转换比/结合得更紧,所以它是7.0 / 2);(double) (7 / 2)→3.0(括号先以int计算7 / 2 = 3,然后加宽)。
最后两个看起来相似但不同——转换的位置决定截断是否发生。
Why int and double store numbers differently
An int holds only whole numbers in a fixed range; a double stores a mantissa and an exponent, trading exactness for a huge range. Casting double→int throws away the fraction, and a value past an int's range overflows.
| 英文 | 中文 | 拼音 |
|---|---|---|
| Casting | 类型转换 | lèi xíng zhuǎn huàn |
1.6
复合赋值运算符
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.6.A |
|
来源:美国大学理事会 AP 课程与考试说明
简写把一个运算与赋值结合:x += 5 意味着 x = x + 5;同样 -=、*=、/=、%=。自增(increment)和自减(decrement)运算符 x++ 和 x-- 加或减一。
1.7
应用程序接口(API)与库
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.7.A |
|
来源:美国大学理事会 AP 课程与考试说明
一个 API(应用程序接口,Application Programming Interface)是你可以使用的类和方法的已发布列表。一个库(library)是一个现成类的搜集(像 Math、String、Scanner)。你读 API 文档以学一个方法需要什么(它的参数)和返回什么,而不看它的内部代码——抽象(abstraction)的一个例子。
| 英文 | 中文 | 拼音 |
|---|---|---|
| library | 库 | kù |
| abstraction | 抽象 | chōu xiàng |
| Interface | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
1.8
用注释编写文档
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.8.A |
|
来源:美国大学理事会 AP 课程与考试说明
注释(comments)被编译器忽略但向人类解释代码:// 用于单行、/* ... */ 用于一个块,而 /** ... */ 用于一个 Javadoc 注释,它记录一个方法的目的、参数和返回值。精确的前置条件(preconditions)和后置条件(postconditions)写在这里。
| 英文 | 中文 | 拼音 |
|---|---|---|
| Comments | 注释 | zhù shì |
1.9
方法签名
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.9.A |
|
1.9.B |
|
来源:美国大学理事会 AP 课程与考试说明
一个方法签名(method signature)是一个方法的名字加它的参数类型,例如 nextInt() 或 substring(int, int)。要调用一个方法你必须提供在数量、类型和顺序上匹配参数的实参(arguments)。完整的方法头也陈述返回类型(return type)——方法返回的值的类型(若没有则是 void)——但返回类型不是签名的一部分,这就是为什么两个方法不能仅凭返回类型不同来区分。
| 英文 | 中文 | 拼音 |
|---|---|---|
| method signature | 方法签名 | fāng fǎ qiān míng |
| arguments | 实参 | shí cān |
1.10
调用类方法
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.10.A |
|
来源:美国大学理事会 AP 课程与考试说明
一个类(静态)方法(class (static) method)属于类本身,所以你在类名上调用它:ClassName.method(args)。不需要对象。
Follow a class-method call on the stack
Calling a class method like Math.max pushes a new frame onto the call stack; when the method returns a value, its frame pops and control goes back to the caller. Step through to watch the stack grow and shrink.
| 英文 | 中文 | 拼音 |
|---|---|---|
| class (static) method | 类方法 | lèi fāng fǎ |
| class | 类 | lèi |
1.11
Math 类
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.11.A |
|
来源:美国大学理事会 AP 课程与考试说明
Math 类提供静态数学方法:Math.abs(x)、Math.pow(base, exp)、Math.sqrt(x),和 Math.random()($[0,1)$ 里的一个 double)。要得到一个从 0 到 n-1 的随机整数:(int)(Math.random() * n)。
1.12
对象:类的实例
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.12.A |
|
1.12.B |
|
来源:美国大学理事会 AP 课程与考试说明
= 复制的是引用,不是对象一个类(class)是一个蓝图;一个对象(object)是从它构建的一个具体的实例(instance)。一个类把数据(字段)与行为(方法)捆绑——面向对象编程(object-oriented programming)的核心。String、Scanner 和 ArrayList 都是你实例化的类。
类可以被组织成一个层级。一个父类(superclass)持有几个子类(subclasses)共享的属性和行为,这些子类 extend(扩展)它——一个继承关系(inheritance relationship)。Java 里每个类最终都是内置 Object 类的一个子类,这就是为什么每个对象已经有一个 toString 方法;写一个与父类方法有相同签名的子类方法就是方法重写(method overriding)。(设计你自己的继承超出本课程,但你要能识别这些词汇。)


| 英文 | 中文 | 拼音 |
|---|---|---|
| object | 对象 | duì xiàng |
| instance | 实例 | shí lì |
| object-oriented programming | 面向对象编程 | miàn xiàng duì xiàng biān chéng |
| superclass | 父类 | fù lèi |
| subclasses | 子类 | zi lèi |
| inheritance relationship | 继承关系 | jì chéng guān xì |
| method overriding | 方法重写 | fāng fǎ zhòng xiě |
| reference | 引用 | yǐn yòng |
1.13
对象的创建与存储(实例化)
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.13.A |
|
1.13.B |
|
1.13.C |
|
来源:美国大学理事会 AP 课程与考试说明
实例化(instantiation)用 new 关键字创建一个对象,它调用一个构造函数(constructor):
Scanner in = new Scanner(System.in);
String s = new String("hi"); // or just "hi"
变量持有一个引用(reference)(对象的地址),不是对象本身。两个引用能指向同一个对象;用 == 比较它们比较地址,不是内容。
一个引用也能指向空:特殊值 null(空值)意味着“不附着于任何对象”。在一个 null 引用上调用一个方法会在运行时以一个 NullPointerException 崩溃。通过用 ==/!= 测试并先检查 null 来防范,这样 && 在方法运行前短路:if (s != null && s.length() > 0)。

| 英文 | 中文 | 拼音 |
|---|---|---|
| Instantiation | 实例化 | shí lì huà |
| constructor | 构造函数 | gòu zào hán shù |
| null | 空值 | kōng zhí |
1.14
调用实例方法
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.14.A |
|
来源:美国大学理事会 AP 课程与考试说明
一个实例方法(instance method)作用于一个特定的对象,所以你在对象引用上调用它:object.method(args)。例:in.nextInt()、word.length()。
| 英文 | 中文 | 拼音 |
|---|---|---|
| instance method | 实例方法 | shí lì fāng fǎ |
1.15
字符串操作
大纲
| Learning Objective | Essential Knowledge |
|---|---|
1.15.A |
|
1.15.B |
|
来源:美国大学理事会 AP 课程与考试说明
String 对象是不可变(immutable)的——方法返回一个新字符串而不是改变原来的。关键方法(所有索引从 0 开始):
s.length(); // number of characters
s.substring(2, 5); // chars at index 2,3,4 (5 excluded)
s.indexOf("ab"); // first position, or -1
s.equals(other); // content comparison (never use == for Strings)
s.compareTo(other); // <0, 0, >0 by dictionary order
考试技能: substring(a, b) 包括索引 a 但排除 b,而字符串比较必须用 .equals,不是 == ——两个最被考查的字符串陷阱。
Worked example. 令 String s = "COMPUTER";(索引 0–7)。那么 s.length() 是 8;s.substring(0, 4) 是 "COMP"(索引 0,1,2,3 ——索引 4 被排除);s.substring(4) 是 "UTER"(从索引 4 到末尾);s.indexOf("PU") 是 3;而 s.indexOf("X") 是 -1(未找到)。数 substring 被排除的端点是单个最常见的失误。
请求一个在 0 到 length()-1 之外的索引(一个坏的 substring 或 charAt 参数,例如这里的 s.substring(0, 20))会以一个 StringIndexOutOfBoundsException 崩溃——数组索引错误的字符串表亲。

Explore string indices and slicing
Every character has an index, and the numbering starts at 0. Drag the start and end to see how substring(from, to) takes the characters from from up to — but not including — to.
| 英文 | 中文 | 拼音 |
|---|---|---|
| immutable | 不可变 | bù kě biàn |
1.15
考试技巧
- 逐行手工跟踪代码,在一张表里追踪每个变量的值——考试奖励仔细的跟踪而不是猜测。
- 知道 Java 的基本类型以及整数除法截断($7/2$ 给出 $3$);用一个转换或一个 double 做实数除法。
- 区分编译时错误(语法、类型)和运行时错误——知道有名字的那些:
ArithmeticException(int÷0)、NullPointerException(在 null 引用上的方法)、StringIndexOutOfBoundsException/ArrayIndexOutOfBoundsException——和逻辑错误(错误的输出)。 - 遵循运算符优先级并在你使用每个变量之前初始化它。
- 在自由回答上,写完整的、可编译的 Java ——返回正确的类型并精确匹配方法头。
本主题的互动课程
逐步学习,并即时检测练习。