Using Objects and Methods
What this unit covers
The topics below follow the published CS A course framework for Unit 1. This unit is worth 15–25% of the exam, so budget your time against that rather than against how long the unit takes to teach.
Lessons in this unit
- Objects & Classes13 min · 3 objectivesDistinguish a class (the blueprint) from an object (an instance built from it) · Create an object with the `new` keyword and a constructor · Call a method on an object using dot notation to read its state
- Method Calls14 min · 3 objectivesDifferentiate methods that return a value from `void` methods that do not · Pass arguments to a method and predict the returned result · Chain method calls, evaluating them left to right
- String Methods14 min · 3 objectivesUse core String methods: `length`, `substring`, `indexOf`, and `equals` · Compare Strings correctly with `.equals` rather than `==` · Predict the result of substring and index operations by counting positions
- Math Class & Wrapper Classes12 min · 3 objectivesCall static Math methods such as `abs`, `max`, `min`, and `pow` · Predict results of integer division and how it differs from double division · Recognize the Integer and Double wrapper classes for boxing primitives
Formulas in Unit 1
Every term in Unit 1
All 43 terms we publish for Using Objects and Methods, with definitions. Reading them through is the fastest way to find the ones you cannot define — then drill those in cram mode until you can produce them without the prompt.
- Integer Division
- In Java, dividing two ints truncates toward zero (e.g., 5 / 2 = 2). To get a decimal, cast one to double: (double) 5 / 2 = 2.5.
- Class vs object
- A class is the blueprint; an object is one instance built from it. Many objects, one class.
- Instantiation
- Creating an object with `new`, which allocates memory and runs a constructor. `Scanner s = new Scanner(System.in);`
- Reference variable
- A variable holding the address of an object rather than the object itself. Two references can point at the same object, so changing it through one is visible through the other.
- null
- A reference that points at no object. Calling a method on it throws a NullPointerException — the commonest run-time error in this course.
- Primitive vs reference types
- Primitives (int, double, boolean, char) store a value directly. Reference types store an address. Assignment copies the value for a primitive and copies the address for a reference.
- int
- A 32-bit whole number. Overflows silently past about 2.1 billion rather than reporting an error.
- double
- A 64-bit floating-point number. Cannot represent most decimals exactly, so comparing two doubles with == is unreliable.
- boolean
- true or false. Java will not accept an int in place of a boolean, unlike some other languages.
- Modulus operator
- a % b gives the remainder. 7 % 2 is 1; n % 2 == 0 tests for even. With a negative left operand the result is negative in Java: −7 % 2 is −1.
- Casting
- Converting between types: (int) 7.9 is 7 — it truncates, it does not round. (double) 7 / 2 is 3.5 because the cast happens before the division.
- Rounding with a cast
- To round a positive double, add 0.5 before casting: (int)(x + 0.5). Casting alone always truncates.
- Compound assignment
- x += 3 is x = x + 3. Also −=, *=, /=, %=. A compound assignment on an int keeps the int truncation behavior.
- Increment and decrement
- x++ and x-- change the variable by one. As an expression, x++ evaluates to the old value and ++x to the new one.
- Operator precedence
- Casts and unary operators first, then * / %, then + −, then relational, then && , then ||, then assignment. Parentheses beat all of it and are worth using.
- String
- An immutable sequence of characters. Every method that appears to change a String actually returns a new one, leaving the original untouched.
- String immutability
- A String object can never be modified. `s.toUpperCase();` on its own line does nothing — the result must be assigned to be kept.
- String concatenation
- The + operator joins Strings. If either operand is a String, the other is converted, so "x" + 1 + 2 is "x12" while 1 + 2 + "x" is "3x".
- length()
- s.length() returns the number of characters. Note the parentheses — arrays use `.length` with no parentheses, and mixing them is a compile error.
- substring(a)
- s.substring(a) returns the characters from index a to the end.
- substring(a, b)
- s.substring(a, b) returns characters from index a up to but NOT including index b. Its length is b − a.
- indexOf
- s.indexOf(str) returns the index of the first occurrence, or −1 if it is not found. The −1 is what questions test.
- equals vs == for Strings
- s.equals(t) compares the characters; s == t compares whether they are the same object. Using == on Strings is the classic silent wrong answer.
- compareTo
- s.compareTo(t) returns a negative number if s comes first in lexicographic order, zero if they are equal, and a positive number if s comes after. Write code that tests only the sign — the AP subset never depends on the magnitude.
- Zero-based indexing in Strings
- The first character is at index 0 and the last is at index length() − 1. Asking for index length() throws StringIndexOutOfBoundsException.
- Math.abs
- Returns the absolute value. Overloaded for int and double, returning the same type it was given.
- Math.pow
- Math.pow(base, exponent) returns a double, always. Assigning it to an int requires a cast.
- Math.sqrt
- Returns the square root as a double.
- Math.random
- Returns a double in the range 0.0 up to but not including 1.0. It never returns 1.0, which is the basis of every random-range question.
- Random integer in a range
- (int)(Math.random() * (max − min + 1)) + min gives an integer from min to max inclusive. Deriving it beats memorizing it, and the exam asks both directions.
- Wrapper classes
- Integer and Double wrap primitive values as objects, so they can go in an ArrayList. Autoboxing converts between them automatically.
- Autoboxing and unboxing
- Java converts int to Integer and back as needed, so `list.add(5)` works on an ArrayList<Integer>. Unboxing a null Integer throws a NullPointerException.
- Method signature
- The method's name and its parameter list. The return type is not part of the signature, which is why two methods cannot differ only in return type.
- Method overloading
- Two methods in a class with the same name and different parameter lists. Java picks by the argument types at the call site.
- void method
- A method that returns nothing. It cannot be used in an expression or assigned to a variable.
- Return type
- The type of value a method sends back. A non-void method must return a value on every path, or the code will not compile.
- Passing a primitive to a method
- The value is copied. Changing the parameter inside the method has no effect on the caller's variable.
- Passing a reference to a method
- The address is copied, so both refer to the same object. The method can change the object's contents, but reassigning the parameter does not affect the caller.
- Scanner
- Reads input. `Scanner in = new Scanner(System.in);` then nextInt(), nextDouble() or nextLine(). Added to the CED in the 2025 redesign.
- nextInt then nextLine
- nextInt() leaves the newline in the buffer, so the next nextLine() returns an empty String. Consuming that leftover line is the standard fix.
- System.out.print vs println
- println adds a newline afterward; print does not. Output-tracing questions depend on this.
- Static method call
- Called on the class, not an object: Math.abs(x). Object methods are called on a reference: s.length().
- Dot operator
- Accesses a member of an object or class. `obj.method()` runs a method; a null reference on the left throws NullPointerException.
What examiners penalize here
- On the exam, `new` always appears with a class that has a matching constructor. If the arguments do not match any constructor’s parameter list, the code fails to compile — check the number and types of arguments carefully.
- When you see chained calls, rewrite them as separate steps on scratch paper: compute the leftmost call, replace it with its result, and repeat. This prevents mis-reading `s.trim().length()` as calling `length()` on the original spaced string.
- For any `indexOf` or `substring` question, write the string out and label each character with its index starting at 0. Counting on paper prevents off-by-one mistakes with the excluded end index.
- `Math` methods are static: always call them as `Math.name(...)`, never on an object. And watch the return type — `Math.pow` returns a `double`, so `Math.pow(2, 3)` is `8.0`, not `8`.
Practice CS A
Our practice bank is drawn from across the whole course rather than filtered to one unit, which is closer to how the exam asks anyway — it will not tell you which unit a question is testing.
Questions about this unit
How much of the AP Computer Science A exam is Unit 1?
Unit 1, Using Objects and Methods, is worth 15–25% of the CS A multiple-choice section according to the published course framework. Across all 4 units that makes it one of the heaviest units on the exam, and worth front-loading.
What topics are covered in CS A Unit 1?
Using Objects and Methods covers Objects & classes, Method calls, String methods and Math class & wrappers. We publish 43 terms with definitions for this unit, all of them on this page.
How should I study CS A Unit 1?
Read the 4 lessons below first — about 55 minutes — then drill the 43 terms in cram mode until you can produce each definition from memory rather than just recognize it. Recognition is what makes a unit feel finished when it is not. Finish with practice questions and read the explanation for every one you get right by elimination as well as the ones you miss.
All 4 units of AP Computer Science A
Unit names, topics and exam weights follow the published College Board course framework for AP Computer Science A. AP® is a trademark registered by the College Board, which does not endorse this site.