AP Computer Science A · Unit 1 · 15-25% of the exam · about 95 minutes
Unit 1: Using Objects and Methods
By the end: Trace any Unit 1 expression, method call or String operation exactly the way Java runs it, and write short methods that call the methods you are given.

- 1.1 Algorithms, programming and compilers · 8 min
From an idea to a running program
- An algorithm is a step-by-step process. Sequencing means the steps run in order, top to bottom.
- The compiler checks your source code and translates it to bytecode. The JVM runs the bytecode.
- Syntax error: caught by the compiler, nothing runs. Run-time error: an exception stops the program. Logic error: it runs and gives a wrong answer.
- 1.2 Variables and data types · 7 min
Every value has a type
- Primitive types: int, double, boolean. Reference types: String and every class. A reference variable holds an arrow to an object.
- Declare before use: type, then name. Names are case-sensitive, camelCase by convention, and can't be keywords.
- An int can go into a double automatically. A double can't go into an int without a cast.
- 1.3 Expressions and output · 10 min
Java does math its own way
- int op int gives an int. If either side is a double, the result is a double.
- int / int truncates toward zero: 17 / 5 is 3, -17 / 5 is -3. % gives the remainder: 17 % 5 is 2.
- *, / and % come before + and -. Same level: left to right.
- 1.4 Assignment and input · 1.5 Casting and range of variables · 10 min
Store it, convert it, know its limits
- Assignment: evaluate the whole right side, then store it on the left. The old value is gone.
- (int) truncates toward zero. (int) (x + 0.5) rounds a nonnegative x.
- A cast applies to the value right after it: (double) 7 / 2 is 3.5, (double) (7 / 2) is 3.0.
- 1.6 Compound assignment operators · 5 min
Shortcuts that update a variable
- 1.7 APIs and libraries · 1.8 Documentation with comments · 1.9 Method signatures · 11 min
Using code you didn't write
- A library is a set of tested classes; its API tells you how to use them. java.lang (String, Math) is always available.
- Comments: //, /* */, /** */. Preconditions are what the caller must make true; postconditions are what the method promises.
- Signature: name plus parameter types in order. Return type: what comes back; void means nothing.
- 1.10 Calling class methods · 1.11 The Math class · 9 min
Methods that belong to the class
- Class (static) methods are called on the class name: Math.sqrt(x).
- Math.abs returns the same type it's given. Math.pow and Math.sqrt return a double.
- Math.random() returns a double with 0.0 <= value < 1.0.
- 1.12 Objects: instances of classes · 1.13 Object creation and storage · 9 min
Blueprints and the things built from them
- A class is a blueprint and a type. An object is one instance, with its own state.
- new runs a constructor and returns a reference to a brand-new object. Constructors have the class's name and no return type.
- Arguments to new must match a constructor signature. No match, no compile.
- 1.14 Calling instance methods · 8 min
Asking an object to do something
- Call instance methods on an object with a dot: a.add(300). They use and change that object's data.
- void methods are statements. Non-void methods return a value: store it or use it.
- Assigning one reference to another (c = a) makes aliases: two names, one object.
- 1.15 String manipulation · 12 min
Strings: text you can slice
- Strings are immutable. Methods return new Strings; assign the result if you want to keep it.
- Indexes run from 0 to length() - 1. Going outside throws StringIndexOutOfBoundsException.
- substring(a, b): from a up to, not including, b (length b - a). substring(a): from a to the end. substring(k, k + 1): one character.
- Unit 1 wrap-up · 9 min
Put it together