AP Computer Science A study materials
AP Computer Science A study resources

AP Computer Science A cheat sheet

AP Computer Science A · Keep the key ideas within reach.

Quick reference

Using Objects and Methods

  • A reference variable stores the memory address of an object, not the object itself; assigning one reference to another copies the address, so both aliases see the same object state.

  • The null reference points to no object; calling any instance method on null throws NullPointerException at run time.

  • A method signature is its name plus the ordered list of parameter types; return type and parameter names are not part of the signature.

  • Java passes all arguments by value: primitives copy the value, object references copy the address, so a method can mutate the referenced object but cannot reassign the caller's variable.

  • The String class is immutable: methods such as substring, toUpperCase, and replace return a new String and never modify the original.

  • String comparison: use equals for content and compareTo for ordering; == compares reference identity and may be false for equal contents.

  • Integer division truncates toward zero; mixing an int and a double promotes the int to double before dividing, so 7/2 is 3 but 7/2.0 is 3.5.

  • A cast narrows a wider type, as in (int) 3.9, which truncates to 3; casting a double to int discards the fractional part rather than rounding.

  • A wrapper class such as Integer converts between primitives and objects; autoboxing wraps an int automatically, and unboxing extracts the primitive value.

Quick reference

Selection and Iteration

  • Every if, else if, else, for, while, and do-while body is either a single statement or a block in braces; braces are required when the body has more than one statement.

  • An else if chain tests conditions top to bottom and executes only the first true branch; later conditions are skipped once one branch runs.

  • The boolean operators are ! (not), && (and), and || (or); && and || short-circuit, so the right operand may not be evaluated.

  • De Morgan's laws: !(a && b) equals !a || !b, and !(a || b) equals !a && !b.

  • A standard for loop header has three parts: initialization, a boolean continuation test, and an update; the test runs before each iteration and the update after each body execution.

  • A while loop tests before each iteration and may run zero times; a do-while loop tests after the body and always runs at least once.

  • The enhanced for loop, for (int x : arr), visits each element in order but cannot be used to remove elements or change the array's length.

  • An off-by-one error occurs when a loop bound is wrong, such as using <= with an index that should stop at length - 1; valid array indices run from 0 to length - 1.

  • A nested loop's total iterations equal the product of the outer and inner iteration counts; the inner loop restarts fully on each outer pass.

Quick reference

Class Creation

  • A class encapsulates instance variables (state) and methods (behavior); private instance variables restrict direct access to code inside the class.

  • A constructor has the same name as the class and no return type; it initializes a newly created object and can be overloaded with different parameter lists.

  • The no-argument constructor is supplied by the compiler only when the class declares no constructors; declaring any constructor removes the default.

  • The this reference refers to the current object; this.field disambiguates an instance variable from a parameter of the same name.

  • A static method or variable belongs to the class rather than an object; static methods cannot access instance variables directly and cannot use this.

  • An accessor (getter) returns information about an object without changing state; a mutator (setter) changes state, often after validating the new value.

  • Method overriding requires the same signature and a compatible return type in a subclass; the subclass version is chosen at run time based on the object's actual type.

  • A subclass constructor implicitly calls the superclass no-argument constructor via super() unless an explicit super(...) call is the first statement.

  • The toString method returns a String description of an object and is called automatically when an object is concatenated with a String.

Quick reference

Data Collections

  • An array has a fixed length set at creation; length is a public final field accessed without parentheses, as in arr.length.

  • Array indices are 0 through length - 1; accessing an index outside that range throws ArrayIndexOutOfBoundsException.

  • Default array element values are 0 for numeric primitives, false for boolean, '\u0000' for char, and null for object references.

  • An ArrayList is a resizable list with methods size(), get(i), set(i, value), add(value), add(i, value), and remove(i); remove returns the removed element.

  • ArrayList indices also run from 0 to size() - 1; calling get(size()) throws IndexOutOfBoundsException.

  • Removing from an ArrayList while iterating forward with an index can skip elements; decrement the index after a removal or iterate backward.

  • The enhanced for loop over a collection cannot modify the collection's size; adding or removing inside it throws ConcurrentModificationException.

  • A 2D array is an array of arrays; arr.length is the number of rows and arr[0].length is the number of columns in the first row.

  • Arrays can be traversed with a standard for loop when indices are needed and with an enhanced for loop when only values are needed; the enhanced loop cannot change elements of a primitive array.