AP Computer Science A Unit 2: Using Objects
Study String class, methods, constructors, wrapper classes, Math class with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
Objects are instances of classes that combine data and behavior. This unit covers creating objects with constructors, calling methods, and working with the String and Math classes from the Java API.
Why it matters
Object-oriented programming is the core paradigm of Java and the AP CSA exam. Understanding how to create objects, call methods, and work with return values is essential for every remaining unit.
Key concepts
- A class is a blueprint; an object is an instance created with the new keyword and a constructor.
- Methods are called using dot notation: object.method(arguments).
- String objects are immutable; methods like substring, indexOf, and length return new values without modifying the original.
- The Math class provides static methods like abs, pow, sqrt, and random.
Creating and Using Objects
An object is created by calling a constructor with the new keyword: Scanner input = new Scanner(System.in). The constructor initializes the object's internal state. Once created, you interact with objects by calling their methods using dot notation. Methods may take parameters and may return values. Void methods perform an action without returning anything. Non-void methods return a value that can be stored, printed, or used in an expression. Understanding the difference is critical for the AP exam.
The String Class
Strings are sequences of characters and are objects in Java. Key methods include length() (number of characters), substring(start, end) (extracts characters from index start to end-1), indexOf(str) (finds the first occurrence), and equals(other) (compares content, not reference). Strings are immutable — calling a method on a String returns a new String without changing the original. String indices are zero-based, so the first character is at index 0. The AP exam frequently tests off-by-one errors with substring.
The Math Class
The Math class provides static methods called without creating an object: Math.abs(-5) returns 5, Math.pow(2, 3) returns 8.0, Math.sqrt(16) returns 4.0. Math.random() returns a double from 0.0 (inclusive) to 1.0 (exclusive). To generate a random integer in a range [min, max], use (int)(Math.random() * (max - min + 1)) + min. The AP exam tests this formula and the distinction between static methods (called on the class) and instance methods (called on objects).
AP exam tip
Use .equals() to compare String content, not ==. The == operator checks whether two references point to the same object in memory, which can give unexpected results with Strings.
Connections to other units
- Unit 4: Loops iterate through String characters using length() and charAt() or substring().
- Unit 5: Writing your own classes means designing the constructors and methods that other code will call.
- Unit 6: Arrays store collections of objects and use zero-based indexing like Strings.