| Learning Objective | Essential Knowledge |
|---|---|
1.1.A |
|
1.1.B |
|
1.1.C |
|
Using Objects and Methods
AP Computer Science A · Topic 1
1.1
Introduction to Algorithms, Programming, and Compilers
Syllabus
Source: College Board AP Course and Exam Description
An algorithm 算法 is a finite, step-by-step procedure that solves a problem. A program 程序 expresses an algorithm in a language a computer can run. Java is compiled 编译: the compiler 编译器 translates your source code into bytecode, which the Java Virtual Machine (JVM) runs. A syntax error 语法错误 (breaking the grammar) is caught by the compiler; a logic error 逻辑错误 (wrong result) is not – the program runs but misbehaves.
A compiler translates the whole program at once; an interpreter runs it line by line
Your Java program is compiled to instructions a CPU like one of these actually runs
| English | Chinese | Pinyin |
|---|---|---|
| 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
Variables and Data Types
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.2.A |
|
1.2.B |
|
Source: College Board AP Course and Exam Description
A variable 变量 is a named box that stores a value of a fixed type 类型. Java's main primitive types 基本类型 are int (whole numbers), double (decimals), and boolean (true/false). Declare with the type first:
Java's basic data types, each storing a different kind of value
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.
| English | Chinese | Pinyin |
|---|---|---|
| variable | 变量 | biàn liàng |
| type | 类型 | lèi xíng |
| primitive types | 基本类型 | jī běn lèi xíng |
1.3
Expressions and Output
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.3.A |
|
1.3.B |
|
1.3.C |
|
Source: College Board AP Course and Exam Description
An expression 表达式 combines values and operators to compute a result: + - * / and % (modulus 取模, the remainder). Integer division truncates: 7 / 2 is 3, while 7 % 2 is 1. Operator precedence follows math (*,/,% before +,-). Print with:
System.out.print("no newline");
System.out.println("with newline");
Dividing an integer by the integer 0 (like 7 / 0) is not allowed and crashes at run time with an ArithmeticException. Inside a string, a backslash marks an escape sequence 转义序列: \" prints a double quote, \\ a single backslash, and \n starts a new line – so System.out.println("She said \"hi\""); prints 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.
| English | Chinese | Pinyin |
|---|---|---|
| expression | 表达式 | biǎo dá shì |
| modulus | 取模 | qǔ mó |
| escape sequence | 转义序列 | zhuǎn yì xù liè |
1.4
Assignment Statements and Input
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.4.A |
|
1.4.B |
|
Source: College Board AP Course and Exam Description
An assignment 赋值 x = expr; evaluates the right side and stores it in the left variable. Read input with a Scanner:
Scanner in = new Scanner(System.in);
int age = in.nextInt();
String name = in.next();
| English | Chinese | Pinyin |
|---|---|---|
| assignment | 赋值 | fù zhí |
1.5
Casting and Range of Variables
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.5.A |
|
1.5.B |
|
1.5.C |
|
Source: College Board AP Course and Exam Description
Each type has a fixed range; an int overflows past about 2.1 billion. Casting 类型转换 converts between types. Widening (int to double) is automatic; narrowing needs an explicit cast, which truncates (does not round):
double avg = (double) total / count; // force real division
int whole = (int) 3.9; // 3, truncated
Exam skill: watch for integer division producing a truncated result when a decimal was expected – cast one operand to double first.
Worked example. Trace each expression:
7 / 2→3(bothint, so division truncates);7.0 / 2→3.5(onedoubleforces real division);7 % 2→1(the remainder);(double) 7 / 2→3.5(the cast binds tighter than/, so it is7.0 / 2);(double) (7 / 2)→3.0(the parentheses compute7 / 2 = 3inintfirst, then widen).
The last two look alike but differ – the position of the cast decides whether the truncation happens.
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.
| English | Chinese | Pinyin |
|---|---|---|
| Casting | 类型转换 | lèi xíng zhuǎn huàn |
1.6
Compound Assignment Operators
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.6.A |
|
Source: College Board AP Course and Exam Description
Shorthands combine an operation with assignment: x += 5 means x = x + 5; likewise -=, *=, /=, %=. The increment and decrement operators x++ and x-- add or subtract one.
1.7
Application Program Interface (API) and Libraries
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.7.A |
|
Source: College Board AP Course and Exam Description
An API (Application Programming Interface) 应用程序接口 is the published list of classes and methods you may use. A library 库 is a collection of ready-made classes (like Math, String, Scanner). You read the API documentation to learn what a method needs (its parameters) and returns, without seeing its inner code – an example of abstraction 抽象.
| English | Chinese | Pinyin |
|---|---|---|
| library | 库 | kù |
| abstraction | 抽象 | chōu xiàng |
| Interface | 应用程序接口 | yìng yòng chéng xù jiē kǒu |
1.8
Documentation with Comments
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.8.A |
|
Source: College Board AP Course and Exam Description
Comments 注释 are ignored by the compiler but explain code to humans: // for a single line, /* ... */ for a block, and /** ... */ for a Javadoc comment that documents a method's purpose, parameters, and return value. Precise preconditions and postconditions are written here.
| English | Chinese | Pinyin |
|---|---|---|
| Comments | 注释 | zhù shì |
1.9
Method Signatures
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.9.A |
|
1.9.B |
|
Source: College Board AP Course and Exam Description
A method signature 方法签名 is a method's name plus its parameter types, e.g. nextInt() or substring(int, int). To call a method you must supply arguments 实参 that match the parameters in number, type, and order. The signature also states the return type – the type of value the method gives back (void if none).
| English | Chinese | Pinyin |
|---|---|---|
| method signature | 方法签名 | fāng fǎ qiān míng |
| arguments | 实参 | shí cān |
1.10
Calling Class Methods
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.10.A |
|
Source: College Board AP Course and Exam Description
A class (static) method 类方法 belongs to the class itself, so you call it on the class name: ClassName.method(args). No object is needed.
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.
| English | Chinese | Pinyin |
|---|---|---|
| class (static) method | 类方法 | lèi fāng fǎ |
1.11
Math Class
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.11.A |
|
Source: College Board AP Course and Exam Description
The Math class provides static math methods: Math.abs(x), Math.pow(base, exp), Math.sqrt(x), and Math.random() (a double in $[0,1)$). To get a random integer from 0 to n-1: (int)(Math.random() * n).
1.12
Objects: Instances of Classes
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.12.A |
|
1.12.B |
|
Source: College Board AP Course and Exam Description
= copies the reference, not the objectA class 类 is a blueprint; an object 对象 is a concrete instance 实例 built from it. A class bundles data (fields) with behavior (methods) – the heart of object-oriented programming 面向对象编程. String, Scanner, and ArrayList are all classes you instantiate.
Classes can be organised into a hierarchy. A superclass 父类 holds attributes and behaviors shared by several subclasses 子类 that extend it – an inheritance relationship 继承关系. Every class in Java is ultimately a subclass of the built-in Object class, which is why every object already has a toString method; writing a subclass method with the same signature as a superclass one is method overriding 方法重写. (Designing your own inheritance is beyond this course, but you are expected to recognise this vocabulary.)
A class diagram: private attributes and public methods
A class is a blueprint; each object is one instance built from it
| English | Chinese | Pinyin |
|---|---|---|
| class | 类 | lèi |
| 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ě |
1.13
Object Creation and Storage (Instantiation)
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.13.A |
|
1.13.B |
|
1.13.C |
|
Source: College Board AP Course and Exam Description
Instantiation 实例化 creates an object with the new keyword, which calls a constructor 构造函数:
Scanner in = new Scanner(System.in);
String s = new String("hi"); // or just "hi"
The variable holds a reference 引用 (the object's address), not the object itself. Two references can point to the same object; comparing them with == compares addresses, not contents.
A reference can also point to nothing: the special value null 空值 means "not attached to any object". Calling a method on a null reference crashes at run time with a NullPointerException. Guard against it by testing with ==/!= and checking null first, so && short-circuits before the method runs: if (s != null && s.length() > 0).
A primitive variable holds its value directly, a reference holds an arrow to the object
| English | Chinese | Pinyin |
|---|---|---|
| Instantiation | 实例化 | shí lì huà |
| constructor | 构造函数 | gòu zào hán shù |
| reference | 引用 | yǐn yòng |
| null | 空值 | kōng zhí |
1.14
Calling Instance Methods
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.14.A |
|
Source: College Board AP Course and Exam Description
An instance method 实例方法 acts on a specific object, so you call it on the object reference: object.method(args). Example: in.nextInt(), word.length().
| English | Chinese | Pinyin |
|---|---|---|
| instance method | 实例方法 | shí lì fāng fǎ |
1.15
String Manipulation
Syllabus
| Learning Objective | Essential Knowledge |
|---|---|
1.15.A |
|
1.15.B |
|
Source: College Board AP Course and Exam Description
String objects are immutable 不可变 – methods return a new string rather than changing the original. Key methods (all indices start at 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
Exam skill: substring(a, b) includes index a but excludes b, and String comparison must use .equals, not == – two of the most-tested String pitfalls.
Worked example. Let String s = "COMPUTER"; (indices 0–7). Then s.length() is 8; s.substring(0, 4) is "COMP" (indices 0,1,2,3 – index 4 excluded); s.substring(4) is "UTER" (from index 4 to the end); s.indexOf("PU") is 3; and s.indexOf("X") is -1 (not found). Counting the excluded endpoint of substring is the single most common slip.
Asking for an index outside 0 to length()-1 (a bad substring or charAt argument, e.g. s.substring(0, 20) here) crashes with a StringIndexOutOfBoundsException – the String cousin of the array-index error.
String indices start at 0
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.
| English | Chinese | Pinyin |
|---|---|---|
| immutable | 不可变 | bù kě biàn |
1.15
Exam tips
- Trace code by hand line by line, tracking each variable's value in a table — the exam rewards careful tracing over guessing.
- Know Java's primitive types and that integer division truncates ($7/2$ gives $3$); use a cast or a double for real division.
- Distinguish compile-time errors (syntax, types) from run-time errors – know the named ones:
ArithmeticException(int÷0),NullPointerException(method on a null reference),StringIndexOutOfBoundsException/ArrayIndexOutOfBoundsException– and logic errors (wrong output). - Follow operator precedence and initialise every variable before you use it.
- On the free-response, write complete, compilable Java — return the right type and match the method header exactly.