2026 AP CSA Java Quick Reference — Methods You Must Memorize
What You Need to Know
On the AP CSA exam, you’re only guaranteed the built-in methods that appear on the AP CSA Java Quick Reference (plus whatever a problem explicitly tells you). This means you must be able to:
- Recognize method signatures (return type + parameters)
- Predict exact return values (especially edge cases)
- Handle String indexing and ArrayList shifting without guessing
- Use Math.random() correctly to generate ranges
Critical rule: You can assume these methods exist and behave exactly as Java specifies. If you use other library methods (like
String.toUpperCase()), you can’t assume they exist unless the question provides them.
The “must-memorize” classes
From the Quick Reference, you should memorize methods for:
- String
- Math
- Integer and Double (wrapper classes)
- ArrayList
Also memorize these non-method essentials:
- Array has a public field
length(not a method) - Indices are 0-based for Strings, arrays, and ArrayLists
Step-by-Step Breakdown
This topic is mostly recall + precise application. Here are the repeatable “micro-procedures” you’ll use constantly.
1) Safely use substring
- Identify indices as 0-based.
- If using
substring(from, to), remember it returns characters at indices:
(thetoindex is excluded). - If using
substring(from), it returns fromfromthrough the end. - Watch for index errors (the exam typically avoids invalid indices unless it’s testing you).
Mini-check: str.substring(a, b) has length .
2) Use indexOf like a pro
- Decide what you’re searching for: a String pattern.
- Use
indexOf(str)to find the first occurrence. - Use
indexOf(str, fromIndex)to find the first occurrence starting atfromIndex. - If the result is , the pattern is not found.
Common pattern: find the second occurrence
- Find first index
i = s.indexOf(target) - Find next:
j = s.indexOf(target, i + 1)
3) Interpret compareTo correctly
s1.compareTo(s2)compares lexicographically (dictionary-like).- Only the sign matters:
- negative →
s1comes befores2 - → equal
- positive →
s1comes afters2
- negative →
- Don’t assume the return is exactly or .
4) Generate random integers with Math.random()
- Know the range:
- To get an integer in :
- multiply by
n, cast to int
- multiply by
int r = (int)(Math.random() * n); // 0..n-1
- To get an integer in (inclusive):
- size is
int r = a + (int)(Math.random() * (b - a + 1));
5) Predict ArrayList shifting (add/remove)
add(index, obj)inserts and shifts old elements atindex...endright.remove(index)deletes and shifts elements after it left.- In loops, shifting can cause you to skip elements or go out of bounds.
Key Formulas, Rules & Facts
String (memorize these signatures + behaviors)
| Method signature | What it does | Must-know notes |
|---|---|---|
int length() | number of characters | indices valid: to |
String substring(int from, int to) | slice of string | includes from, excludes to () |
String substring(int from) | slice to end | includes from through last char |
int indexOf(String str) | first match index | returns if not found |
int indexOf(String str, int fromIndex) | first match at/after index | fromIndex is a position to start searching |
int compareTo(String other) | lexicographic compare | negative / / positive; not just |
boolean equals(Object other) | content equality | use for Strings, not == |
String indexing facts
- First char is index .
- Last char index is .
Math (you must know return types + ranges)
| Method signature | What it does | Notes |
|---|---|---|
static int abs(int x) | absolute value | returns int |
static double abs(double x) | absolute value | returns double |
static double pow(double base, double exponent) | exponentiation | returns double even for integer-looking inputs |
static double sqrt(double x) | square root | returns double |
static double random() | uniform random double |
Wrapper classes: Integer and Double
These show up when code stores primitives in collections (like ArrayList<Integer>).
| Class | Method / constructor | What it does | Notes |
|---|---|---|---|
Integer | Integer(int value) | constructs an Integer object | often replaced by autoboxing, but still tested conceptually |
Integer | int intValue() | unwrap to int | unboxing sometimes implicit |
Integer | static int parseInt(String s) | converts String to primitive int | assume valid numeric strings unless stated |
Double | Double(double value) | constructs a Double object | conceptually like Integer |
Double | double doubleValue() | unwrap to double | |
Double | static double parseDouble(String s) | converts String to primitive double | assume valid numeric strings unless stated |
ArrayList
| Method signature | What it does | Notes |
|---|---|---|
int size() | number of elements | last index is |
boolean add(E obj) | append to end | returns true (typically ignored) |
void add(int index, E obj) | insert at index | shifts right (existing elements move up) |
E get(int index) | element at index | does not remove |
E set(int index, E obj) | replace at index | returns old element |
E remove(int index) | remove at index | shifts left; returns removed element |
Arrays (not methods, but always tested)
arr.lengthis a field, not a method.- Correct:
arr.length - Wrong:
arr.length()
- Correct:
Examples & Applications
Example 1: substring boundaries
String s = "computer";
System.out.println(s.substring(1, 4));
System.out.println(s.substring(3));
Key insight: substring(1,4) is indices .
- Output:
ompputer
Example 2: Finding multiple occurrences with indexOf
String s = "bananas";
int first = s.indexOf("an");
int second = s.indexOf("an", first + 1);
System.out.println(first + "," + second);
Key insight: searching again must start after the previous match.
firstis , second is → prints1,3
Example 3: compareTo sign (not exact value)
String a = "ape";
String b = "apple";
System.out.println(a.compareTo(b) < 0);
System.out.println(b.compareTo(a) > 0);
System.out.println(a.compareTo("ape") == 0);
Key insight: Only check < 0, > 0, or == 0.
- Outputs:
true,true,true
Example 4: ArrayList shifting trap
ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
nums.add(1, 99); // insert at index 1
nums.remove(2); // remove element currently at index 2
System.out.println(nums);
Step through:
- After insert:
[10, 99, 20, 30] - After remove index : removes
20→[10, 99, 30]
Example 5: Random integer in an inclusive range
Generate through inclusive:
int r = 5 + (int)(Math.random() * (9 - 5 + 1));
Key insight: range size is values.
Common Mistakes & Traps
Using
==for Strings- Wrong:
if (s1 == s2) - Why wrong: compares references, not content.
- Fix:
s1.equals(s2).
- Wrong:
Forgetting
substringexcludes the end index- Wrong thought: “
substring(2,5)gives 4 chars.” - Reality: length is .
- Fix: remember .
- Wrong thought: “
Assuming
compareToreturns only- Wrong:
if (s1.compareTo(s2) == -1) - Why wrong: any negative number means “before.”
- Fix: use
< 0,== 0,> 0.
- Wrong:
Not handling
indexOfreturning- Wrong: using the result as an index without checking.
- Why wrong:
-1is not a valid index. - Fix:
int i = s.indexOf(t); if (i != -1) { ... }.
Calling
arr.length()like a method- Wrong:
arr.length() - Why wrong: arrays use a field.
- Fix:
arr.length.
- Wrong:
Off-by-one with last valid index
- Wrong loop:
for (int i = 0; i <= list.size(); i++) - Why wrong: last index is .
- Fix:
i < list.size().
- Wrong loop:
Breaking loops by modifying an ArrayList while iterating forward
- What happens:
remove(i)shifts elements left, so the next element moves into indexiand you skip it. - Fix options:
- iterate backward (
for (int i = size-1; i >= 0; i--)), or - adjust index carefully.
- iterate backward (
- What happens:
Wrong random range due to missing cast parentheses
- Wrong:
java int r = (int)Math.random() * n; // always 0 - Why wrong:
(int)Math.random()is always . - Fix:
java int r = (int)(Math.random() * n);
- Wrong:
Memory Aids & Quick Tricks
| Trick / mnemonic | Helps you remember | When to use |
|---|---|---|
| Substring is “start inclusive, end exclusive” | substring(from,to) excludes to | Any substring boundary question |
| “IndexOf: -1 means ‘nope’” | not-found value is | Before using indexOf result as an index |
| CompareTo = sign check | use <0, ==0, >0 only | Conditionals with lexicographic order |
| Random: multiply then cast | casting too early breaks it | Any random int generation |
| ArrayList add/remove SHIFT | insert shifts right, remove shifts left | Tracing list mutations |
| Last valid index = size/length minus 1 | avoid off-by-one | Loops and boundary checks |
Quick Review Checklist
- You can list all Quick Reference methods for String, Math, Integer, Double, ArrayList from memory.
- You remember String/array/list indices start at .
- You know
substring(from,to)is and has length . - You know
indexOf(...)returns when not found. - You use
equals(not==) for String content. - You interpret
compareToby its sign, not its exact value. - You know
Math.random()returns . - You can generate a random integer in and .
- You can predict how
ArrayList.add(index, ...)andremove(index)shift elements.
You’ve got this—if you can trace these methods accurately, you’re covering a huge chunk of AP CSA points.