Unit 2: Using Objects

String methods, Math class, wrapper classes, and object instantiation

Unit Resources

Select a resource below to start studying.

📚Study Guide: Using Objects

Unit 2: Using Objects

Java is an object-oriented language, and this unit introduces students to the power of objects and classes. Unlike primitive types, objects are reference types that encapsulate data (fields) and behavior (methods). Students learn to create objects using constructors, call methods using the dot operator, and understand the critical difference between reference variables and primitive variables. The String class receives special emphasis because strings are immutable sequences of characters with a rich library of methods (substring, indexOf, length, equals, compareTo). Understanding string indexing (0-based) and the substring(begin, end) method--which includes begin but excludes end--is essential for parsing and manipulating text. The Math class provides static methods for common mathematical operations (Math.abs, Math.pow, Math.sqrt, Math.random) without requiring object instantiation. The AP exam frequently presents code involving object creation, method chaining, and String manipulation. Students must also grasp the concept of null references and the consequences of calling methods on a null reference (NullPointerException). This unit bridges primitive computation with the object-oriented paradigm that dominates the rest of the course.

Key Concepts

  • Reference Types vs. Primitive Types: Primitives store values directly. Reference variables store memory addresses (references) to objects. Multiple references can point to the same object.
  • Object Instantiation: Use the new keyword with a constructor: ClassName obj = new ClassName(parameters); This allocates memory and initializes the object.
  • String Class: Immutable sequence of characters. Key methods: length(), substring(int begin), substring(int begin, int end), indexOf(String str), equals(Object obj), compareTo(String other). String concatenation with + creates a new String object.
  • String Indexing and Substring: Indices start at 0. substring(2, 5) returns characters at indices 2, 3, and 4. substring(2) returns from index 2 to the end.
  • Math Class: Static methods: Math.abs(x), Math.pow(base, exp), Math.sqrt(x), Math.random() returns [0.0, 1.0). No instantiation needed: Math.methodName().
  • Wrapper Classes: Integer, Double, Boolean wrap primitives as objects. Useful for collections and methods requiring objects. Auto-boxing and unboxing convert automatically.

Vocabulary

  • Reference Variable: A variable that stores the memory address of an object rather than the object's data itself.
  • Constructor: A special method called when an object is instantiated using new; it initializes the object's fields.
  • Immutable: An object whose state cannot be modified after creation; String objects in Java are immutable.
  • Static Method: A method belonging to the class rather than any instance; called using the class name (e.g., Math.random()).
  • Null: A special reference value indicating that a reference variable does not point to any object.
  • NullPointerException: A runtime error thrown when a program attempts to use a null reference to access an object member.

Essential Formulas / Syntax Patterns

  • String s = "Hello"; int len = s.length(); // 5
  • String sub = s.substring(1, 4); // "ell"
  • int idx = s.indexOf("l"); // 2
  • double r = Math.random(); // [0.0, 1.0)
  • int randInt = (int) (Math.random() * 10) + 1; // 1 to 10 inclusive
  • String t = new String("Hello"); // explicit construction

Common Mistakes

  • Using == to Compare Strings: == compares references (memory addresses), not content. Use .equals() to compare String values: s1.equals(s2).
  • Off-By-One in Substring: Remember substring(begin, end) includes begin but EXCLUDES end. substring(0, s.length()) returns the whole string.
  • Forgetting Strings Are Immutable: s.toUpperCase() returns a NEW string; it does not modify s. You must assign the result: s = s.toUpperCase();
  • Calling Methods on Null: String s = null; int x = s.length(); throws NullPointerException. Always check if (s != null) before calling methods.

AP Exam Strategies

  • Trace String Methods Step-by-Step: Write the original string with indices labeled above each character before applying substring or indexOf.
  • Use .equals() for String Comparison: In any conditional or loop testing string equality, use .equals() or .equalsIgnoreCase(), never ==.
  • Generate Random Ranges Correctly: To get integers from min to max inclusive: (int) (Math.random() * (max - min + 1)) + min.
  • Recognize Method Chaining: s.trim().substring(0, 3).toUpperCase() executes left to right; each method returns a new String that becomes the receiver of the next method.

Real-World Applications

  • Text Processing: Search engines, word processors, and compilers rely heavily on String manipulation for parsing, tokenization, and formatting.
  • Simulation and Gaming: Math.random() underlies procedural generation, dice rolls, and Monte Carlo simulations in scientific computing.
  • Data Serialization: Wrapper classes like Integer and Double are essential when converting primitive data to JSON, XML, or database-compatible formats.

Practice Quiz: Using Objects

Answer each question one at a time. Click an option to select your answer.

Question 1 of 150
Question
Loading...
Click to flip
Answer
Loading...
Click to flip back 🔀 Shuffle
1 / 37

🎥Free Video Lessons: Using Objects

Watch these unit review videos directly on our site.

AP CSA Review in 45 minutes (Units 1-7) by Cobi Computer Science

AP Computer Science A Unit 2 Review - Objects - Classes - Method Calls - Strings - Math Class by Meek Extra Help

AP CSA Unit 2 Review: Using Objects by WeService Tsinglan

📄Cheat Sheet: Using Objects

Quick reference for Using Objects. Print this out and review before the exam!

Unit 2: Using Objects - Cheat Sheet

String Methods

  • length() -> number of characters
  • substring(begin) -> from begin to end
  • substring(begin, end) -> [begin, end)
  • indexOf(str) -> first index of str, or -1
  • equals(other) -> true if same characters
  • compareTo(other) -> negative, 0, positive

Math Class (Static Methods)

  • Math.abs(x)
  • Math.pow(base, exp)
  • Math.sqrt(x)
  • Math.random() -> [0.0, 1.0)
  • Random int formula: (int)(Math.random() * range) + min

Reference vs. Primitive

  • Primitive: stores value directly (int, double)
  • Reference: stores memory address of object
  • Multiple references can point to same object

Common Patterns

  • String comparison: use .equals(), not ==
  • Strings are immutable; methods return new Strings
  • Calling method on null -> NullPointerException

Problem-Solving Quick Reference

  • Label indices: H e l l o -> 0 1 2 3 4
  • Random 1-6: (int)(Math.random() * 6) + 1
  • To avoid off-by-one: check with small strings

🔬Ultimate Review Packet Materials

Download official review materials for this unit.

No URP materials available for this unit yet.

Check back soon for study guides, practice questions, and review videos.

← Back to AP Computer Science A