AP Computer Science A — Unit 3: Strings
Ultimate Strict-Grader Study Guide
This guide summarizes every major topic from the AP CSA Unit 3 (Strings) module. It’s concise but complete —
designed for strict graders who expect accurate syntax, precise terminology, and full understanding of Java
String behavior.
1. String Basics
• A String in Java is an object (not a primitive). It represents a sequence of characters enclosed in double
quotes.
• Strings are immutable: once created, their content cannot change.
String s1 = "hello";
String s2 = new String("hello");
[Link](s1 == s2); // false
[Link]([Link](s2)); // true
2. Memory and Immutability
When you modify a String, Java creates a new object in memory. The original String remains unchanged.
String word = "cat";
[Link]("s"); // returns "cats" but word is still "cat"
word = word + "s"; // reassigns word to new value "cats"
3. Indexing and Accessing Characters
• Indexing starts at 0. The last index is length() - 1.
• charAt(i) returns the character at position i. Throws IndexOutOfBoundsException if invalid index.
String str = "Java";
[Link]([Link](0)); // "J"
[Link]([Link]([Link]()-1)); // "a"
4. Common String Methods
Method Description / Example
length() Returns number of characters. Example: 'hi'.length() → 2
substring(a,b) Returns substring from a (inclusive) to b (exclusive).
charAt(i) Character at index i.
indexOf(x) First index of character or substring x.
equals(str) Checks if contents are identical (case-sensitive).
equalsIgnoreCase(str) Checks equality ignoring case.
compareTo(str) Compares lexicographically; returns negative/0/positive.
toLowerCase(), toUpperCase()
Return new String in lowercase/uppercase.
contains(sub) Checks if substring exists within String.
concat(str) Concatenates strings (same as +).
5. compareTo() and Lexicographic Order
compareTo() compares two Strings alphabetically based on Unicode values.
"apple".compareTo("banana") // negative
"dog".compareTo("cat") // positive
"bat".compareTo("bat") // 0
Rules: • <0 if first string comes before second. • 0 if identical. • >0 if first comes after. • Case-sensitive:
uppercase < lowercase.
6. Concatenation and Formatting
Use + or concat() to combine strings. Use printf() for formatted output.
String name = "Alex";
int age = 17;
[Link]("%s is %d years old.", name, age);
// Output: Alex is 17 years old.
7. Common Pitfalls (Strict-Grader Notes)
• Using == instead of equals() for content comparison.
• Off-by-one errors with substring or charAt.
• Forgetting immutability (assuming original String changes).
• Not handling uppercase/lowercase differences.
• Misusing [Link]() vs nextLine().
8. FRQ Example: Remove Vowels
public static String removeVowels(String s) {
if (s == null) return null;
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
char c = [Link]([Link](i));
if ("aeiou".indexOf(c) == -1) [Link]([Link](i));
}
return [Link]();
}
Explanation: O(n) runtime; uses StringBuilder for efficiency. Handles empty and null inputs safely.
9. Quick Cheatsheet
equals() content equality
compareTo() lexicographic comparison
length() string size
charAt(i) character at index i
substring(a,b) slice from a to b-1
contains(x) checks substring
toLowerCase() new lowercase string
[Link](s) convert to integer
10. Final Pre-Test Checklist
✓ Know all String methods and return types. ✓ Practice charAt and substring index rules. ✓ Use equals(), not
==. ✓ Handle empty and null Strings. ✓ Explain time complexity (O(n)). ✓ Include comments for clarity.