📖 Crammy · All study guides
AP Computer Science A · Unit 2

Using Objects: every key term you need (+ practice quiz)

56 flashcard terms for AP Computer Science A Unit 2, written to match the course framework. Read them here, drill them as flashcards, or take the 44-question quiz. Free, no account needed.

Study this unit free →

More AP Computer Science A guides

Objects
Instance of a class; contains data (fields) and behavior (methods). Created with new keyword: Dog myDog = new Dog();
Classes
Blueprint for objects; defines fields (data) and methods (behavior). Example: class Dog { int age; void bark() {} }
References vs Primitives
Primitive: stores actual value. Reference (object): stores memory address pointing to object. Different behavior for assignment/comparison.
null Reference
Reference pointing to no object. Causes NullPointerException if you try to use it. Check with if (obj != null).
String Class
Immutable sequence of characters. Methods: length(), charAt(index), substring(start, end), indexOf(char), equals(), compareTo().
String Concatenation
+ operator joins strings: \"Hello\" + \"World\" = \"HelloWorld\". Also converts primitives: \"Age: \" + 25 = \"Age: 25\".
String Methods
toUpperCase(), toLowerCase(), trim(), split(delimiter), startsWith(), endsWith(), replace(). Each returns new String (immutable).
ArrayList
Resizable array; stores object references. ArrayList<Integer> list = new ArrayList<>(); Access with get(index), add(element), remove(index).
ArrayList vs Array
Array: fixed size, faster. ArrayList: dynamic size, slower. ArrayList more convenient for unknown sizes.
Method Calling
object.method(arguments). Example: myString.length() returns length. Methods act on object (implicit 'this').
Constructor
Special method creating objects; same name as class. Can have parameters. Default no-arg constructor provided if none written.
toString() Method
Returns String representation of object; automatically called when printing. Override in custom classes for meaningful output.
Unit 2 Summary
Objects contain data (fields) and methods. References store memory addresses. Strings immutable; ArrayList resizable. Constructors initialize objects.
Reference Aliasing
After Point p2 = p1; both variables refer to the same object. A change made through p2 is visible through p1. Reassigning p2 to a new object breaks the alias.
== vs equals for Objects
== compares references (same object?), equals compares content as defined by the class. Two distinct String objects with the same characters are equals but not ==.
String Immutability Consequence
Methods like toUpperCase or substring return a new String; the original is unchanged. s.toUpperCase(); alone does nothing useful; you must write s = s.toUpperCase();
substring(from, to)
Returns characters from index from up to but not including to. Length of the result is to - from. substring(from) alone goes to the end of the string.
indexOf
Returns the index of the first occurrence of a substring, or -1 if absent. Commonly used to test membership: if (s.indexOf("cat") != -1).
Drill these as interactive flashcards →
compareTo
s1.compareTo(s2) returns negative if s1 comes before s2 lexicographically, 0 if equal, positive if after. Uppercase letters sort before lowercase because of Unicode values.
StringIndexOutOfBoundsException
Thrown when substring or charAt uses an index below 0 or beyond the string length. Valid indices run from 0 to length() - 1; substring's second argument may equal length().
Wrapper Classes Integer and Double
Object versions of int and double, needed because ArrayList holds only objects. Autoboxing converts int to Integer automatically and unboxing converts back.
Autoboxing Pitfall
Comparing two Integer objects with == compares references, which may be false for equal large values. Use equals or unbox to int before comparing.
Math.random Range Formula
(int)(Math.random() * (hi - lo + 1)) + lo produces a random int from lo to hi inclusive. Math.random() itself returns a double in [0.0, 1.0).
Math.pow and Math.sqrt
Both return double even for integer arguments; Math.pow(2, 3) is 8.0. Cast to int if an int is required and you know the result is whole.
Math.abs Overloads
Math.abs(int) returns int and Math.abs(double) returns double. Overload resolution is by argument type at compile time.
Method Signature
The method name plus the number, types, and order of its parameters. Return type is not part of the signature; two methods cannot differ only by return type.
Static Method Call
Called on the class, not on an object: Math.sqrt(16), Integer.parseInt("42"). No object state is used.
Instance Method Call
Requires an object: s.length(), obj.getName(). Calling an instance method on a null reference throws NullPointerException.
Constructor Overloading
A class may provide several constructors with different parameter lists. new Rectangle() and new Rectangle(3, 4) choose the constructor by matching arguments.
Void vs Non-void Methods
A void method performs an action and returns nothing, so it cannot appear inside an expression. A non-void method returns a value that can be assigned, printed, or passed on.
Procedural Abstraction
Using a method by knowing what it does (its documentation) without knowing how. The AP exam gives method descriptions and expects you to call them correctly.
Escape Sequences
\n newline, \t tab, \" embedded double quote, \\ backslash. Each counts as one character in length().
String Concatenation with Numbers
Evaluated left to right: "Sum: " + 1 + 2 prints Sum: 12, while "Sum: " + (1 + 2) prints Sum: 3.
Integer.MIN_VALUE Sentinel
Initialize a running maximum to Integer.MIN_VALUE so that any real value replaces it on the first comparison; use MAX_VALUE for a running minimum.
Reference Copy Semantics
Assigning one object variable to another copies the reference, so both names reach the same object and a mutation through either is visible through both.
null Reference vs Empty Object
A null reference points at no object at all; calling any method on it throws NullPointerException. An empty String is a real object of length 0.
Test yourself on this unit →
String Pool and == Surprises
Two identical String literals may share one pooled object so == is true, while new String("hi") creates a distinct object and == is false. Always compare with equals.
equals Compares Content for String
String overrides equals to compare characters in order, which is why "abc".equals("abc") is true even for separately built objects.
compareTo Return Value Meaning
Returns a negative number, zero, or a positive number. Only the sign is specified by contract; the exact magnitude should never be relied upon.
substring Half-open Interval
s.substring(a, b) includes index a and excludes index b, so its length is b - a and s.substring(i, i) is the empty String.
indexOf Returns -1 for Absent
A missing substring yields -1, which is why the result must be tested before it is used as an index.
Chained String Calls
s.substring(1).toUpperCase() applies each call to the value returned by the previous one, and the original s is never modified.
Immutability Means Reassignment
s.toUpperCase() returns a new String; without s = s.toUpperCase() the original variable still holds the old text.
Method Call on an Expression
You may call a method directly on any expression that evaluates to an object, as in getName().length(), without storing an intermediate variable.
Static Method Belongs to the Class
Math.sqrt is called through the class name because it needs no object state; instance methods require a receiver object.
Math.random Scaling
(int)(Math.random() * (high - low + 1)) + low produces a uniform integer from low through high inclusive.
Math.pow Returns double Always
Even Math.pow(2, 3) returns 8.0, so an int result requires a cast and awareness of tiny rounding error for large exponents.
Integer Wrapper Caching
Integer values from -128 to 127 may be cached, so == can be true for small boxed values and false for large ones. Use equals or intValue.
Autoboxing at Call Sites
Passing an int where an Integer is expected boxes automatically; passing null where an int is expected throws NullPointerException on unboxing.
Constructor Selection by Signature
Overloaded constructors are chosen by the number and types of arguments, resolved at compile time.
Object State Lives in the Heap
The variable holds a reference on the stack while the fields live in the heap, which is why passing the reference to a method can change the object's state.
toString Called Implicitly
System.out.println(obj) and string concatenation both invoke obj.toString(), so a missing override prints a class name and hash code.
Documentation-driven Use
Using a class correctly requires only its public interface — the method signatures, preconditions, and return descriptions — not its implementation.
Aliasing Through a Method Return
Returning a reference to a private mutable field lets callers modify the object's internals; returning a copy prevents that leak.
Ask the AI tutor about this unit →
length() vs length
String uses the method length(), arrays use the field length, and ArrayList uses size(); mixing them is a compile error.
Escape Sequences in Literals
Backslash-n is a newline, backslash-t a tab, backslash-quote a double quote, and two backslashes are a single backslash character.
Turn these into flashcards & quizzes →