The String Class and Immutability

AP Computer Science A · Unit 1 — Using Objects and Methods

Strings never change; String methods build new Strings

A String object is immutable. No method in the class edits the characters it already holds; every method that looks like an edit actually returns a brand-new String and leaves the original alone. So s.toUpperCase(); on its own does nothing useful, while s = s.toUpperCase(); does what you meant.

The methods in the AP subset, with the exact conventions they use:

Because a String variable is a reference, == asks “same object?” while equals asks “same characters?”. Two Strings built at run time can hold identical text and still fail ==. Comparing Strings with == is the single most common silent bug in this unit.

The string “COMPUTER” and the index of each character C O M P U T E R 0123 4567 substring(2, 5) → “MPU”, length 5 − 2 = 3 characters position 5 is OUTSIDE the cut — that is why “T” is absent indexOf(“PU”) = 3 · indexOf(“Z”) = −1 · length() = 8
A counting trick that never fails. Do not count characters, count the dividers: divider number 0 sits before the first letter, divider number n sits after the nth letter. substring(a, b) cuts exactly the segment between divider a and divider b, so the length is always b - a and you do not need to remember “inclusive or exclusive”. Thanks to this way of counting, s.substring(0, s.length()) gives the whole string, and s.substring(s.length()) gives an empty string rather than an error.

Worked example: count the dividers, then compare with equals

Given String s = "Computer Science";. Determine the value printed by each line, then explain why the last line prints false.

System.out.println(s.substring(9, 12));
System.out.println(s.indexOf("Sci") + " " + s.indexOf("sci"));
String t = s.substring(0, 8);
String u = "Computer";
System.out.println(t.equals(u) + " " + (t == u));

Solution

Line 1. Count the tick marks: mark 9 sits right before the letter S, and mark 12 sits after three characters. The result is Sci, whose length is exactly 12 - 9 = 3.

Line 2. indexOf is case-sensitive. The substring Sci starts at position 9. The lowercase sci does not appear in the string, so it returns -1. The line prints 9 -1.

Lines 3 and 4. t is a new String built by substring at run time, while u is a String literal written directly in the code. They are two different objects with the same sequence of characters.

Line 5. t.equals(u) compares character by character, so it gives true. But t == u compares the addresses of the two objects, so it gives false. The line prints true false.

This is exactly the most dangerous situation: if you write a program with == and test it with string literals, it works correctly, but once the data comes from the keyboard or from substring it is wrong, and no error message appears.

Common mistake

Comparing two strings with ==. It may give true while you are testing, because Java merges identical string literals into one object, and then give false in real use. How to prevent it: set a hard rule for yourself — on the CS A exam, compare two strings only with equals or compareTo; use == only for numbers and for null. A related trap: forgetting that substring(a, b) excludes position b, and calling substring with b greater than length() throws StringIndexOutOfBoundsException at run time.

Key takeaway

String is immutable: every method returns a new string, so you must assign it back for it to have any effect. substring(a, b) includes a and excludes b, with length b - a. indexOf returns -1 when it finds nothing. Compare strings with equals, never with ==.

← Calling Methods: Parameters and Return Values · All AP Computer Science A lessons · Practice AP Computer Science A questions (free, instant scoring) · Math, Wrapper Classes, Casting and Overflow →