| Study Guides
AP Computer Science A · Using Objects · 12 min read · Updated 2026-05-09

Using Objects — AP Computer Science A Study Guide

For: AP CS A candidates sitting AP Computer Science A.

Covers: Object instantiation with new, calling instance methods, String class methods, Math class methods, primitive vs reference types, autoboxing, scope of variables — AP Computer Science A Unit 2.

You should already know: Primitive types and arithmetic (Unit 1).

A note on the practice questions: All worked questions in the "Practice Questions" section below are original problems written by us in the AP Computer Science A style for educational use. They are not reproductions of past College Board papers.


1. Why Using Objects Matters

Unit 2 is the bridge from primitives to OO programming. Until now you only had int, double, boolean, char — the four primitive types. Unit 2 introduces objects (instances of classes) and the methods you call on them. About 5-7.5% of the AP CS A score comes directly from Unit 2, but objects underlie every later unit.

Two big shifts:

  1. Reference types vs primitive types — references point to objects, not store values directly.
  2. Method calls vs operators — s.length() is a method call; i + 1 is an operator. You'll spend Unit 5 (Writing Classes) writing the methods you use here.

2. Class instantiation with new

To create an object you write:

String s = new String("hello");
Random r = new Random();

The variable on the left holds a reference (memory address). The right side calls a constructor to allocate and initialize the object. After the assignment, s doesn't store "hello" — it stores a pointer to where "hello" lives.

For some classes (notably String), Java allows a literal shortcut: String s = "hello" is equivalent to String s = new String("hello") for most purposes.

Multiple variables can point to the same object: String t = s; means s and t are aliases — modifying through one affects the other (when the object is mutable).

3. Calling instance methods

Once you have an object reference, call its methods with dot notation:

String s = "Hello, World!";
int len = s.length();         // 13
String upper = s.toUpperCase(); // "HELLO, WORLD!"
char c = s.charAt(7);         // 'W'

Method signature parts:

  • Receiver — the object the method runs on (s).
  • Namelength, toUpperCase, etc.
  • Arguments — values in parentheses.
  • Return — what the method gives back (assigned to len, upper, c).

A void method has no return: s.intern() (rarely used).

4. The String class — high frequency on AP

Memorise these methods (test guarantees them every year):

Method Returns Example
length() int (string length) "abc".length() → 3
charAt(i) char at index i (0-based) "abc".charAt(1) → 'b'
substring(a) substring from index a to end "hello".substring(2) → "llo"
substring(a,b) substring from a (inclusive) to b (exclusive) "hello".substring(1,4) → "ell"
indexOf(c) first index of char/string, −1 if absent "hello".indexOf('l') → 2
equals(other) true if string equal "abc".equals("abc") → true
compareTo(other) int: <0, 0, >0 "a".compareTo("b") → negative
concat(other) concatenated string (or use + operator)

Strings are immutable: s.toUpperCase() returns a new string, doesn't change s. To save the change you must reassign: s = s.toUpperCase().

5. The Math class — static methods

Math methods are static — call without an instance: Math.sqrt(25), not m.sqrt(25).

Common ones:

Method Returns Example
Math.abs(x) absolute value Math.abs(-3) → 3
Math.pow(b,e) b^e (double) Math.pow(2,10) → 1024.0
Math.sqrt(x) square root (double) Math.sqrt(16) → 4.0
Math.random() random double in [0,1) for use (int)(Math.random() * (b-a)) + a

6. Primitive vs reference types

Primitives: int, double, boolean, char — store values directly. Compared with ==. Pass-by-value (a copy is sent).

References: anything starting with capital letter (String, Integer, int[], custom classes) — store memory addresses. Compared with == (same reference?) or .equals() (same content). Pass-by-value of the reference (the pointer is copied; both copies point to the same object).

String s1 = "abc"; String s2 = "abc"; s1 == s2 is sometimes true (Java pools literals) but s1.equals(s2) is always true. Always use .equals() for string content comparison on AP.

Autoboxing: Integer i = 5; auto-converts the primitive 5 into an Integer object. Useful in collections that only accept references (ArrayList<Integer>).

7. Worked Example

What does this code print?

String s1 = "Hello";
String s2 = s1;
s2 = s2 + " World";
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.length() + " vs " + s2.length());

Solution.

Line 1: s1 references "Hello". Line 2: s2 is now an alias of s1 — both point to "Hello". Line 3: s2 + " World" creates a new string "Hello World". s2 is reassigned to point at the new string. s1 still points at "Hello" (immutable strings, plus reassignment doesn't affect s1).

Output:

Hello
Hello World
5 vs 11

8. Common Pitfalls

  • == vs .equals() for strings: "abc" == new String("abc") is false (different objects). Use .equals() for content comparison.
  • String immutability: s.toUpperCase() doesn't modify s — must reassign s = s.toUpperCase().
  • Off-by-one on substring/indexOf: substring(a, b) is exclusive on b. "abcde".substring(1, 4) is "bcd" not "bcde".
  • Aliasing: when you assign one reference to another, both point to the same object. Modifying through one affects what the other sees (only matters for mutable objects, e.g. arrays).

9. Practice Questions (CED Style)

  1. Write a code segment that takes a String name and returns the first letter capitalised plus the rest in lowercase (e.g. "hELLo" → "Hello").
  2. Given String s = "abcabc";, what is the value of s.indexOf("c")? What about s.indexOf("c", 3) (which searches from index 3)?
  3. Write code that uses Math methods to round a double x to the nearest hundredth (e.g. 3.14159 → 3.14).

10. Quick Reference Cheatsheet

  • new instantiates: String s = new String("hello"); (literal shortcut: String s = "hello";).
  • Dot calls method: s.length(), s.substring(2,5).
  • String is immutable: methods return new strings, don't mutate.
  • == compares references; .equals() compares content.
  • Math is static: Math.sqrt(x), no new.
  • substring(a,b) exclusive on b.
  • Autoboxing auto-wraps primitives into objects for collections.

11. What's Next

Using Objects sets up Unit 5 (Writing Classes) where you create your own classes and instance methods. Unit 7 (ArrayList) builds on String[] and similar reference types. Use Ollie to step through any string manipulation FRQ — these are very high yield: "Show me how to reverse a string with substring/charAt" or "Why does my .equals() return false when the strings look the same?"

← Back to topic

Stuck on a specific question?
Snap a photo or paste your problem — Ollie (our AI tutor) walks through it step-by-step with diagrams.
Try Ollie free →