TOPIC 1.15 · STUDY
String Manipulation
String objects never change their characters in place. Methods such as substring return a new String; a variable updates only when you assign that result. Characters are indexed from 0. length counts characters, substring uses half-open bounds, indexOf finds the first match or returns -1, and equals and compareTo compare contents case-sensitively.
1.15.A
String immutability
A String object is immutable: once it exists, its sequence of characters does not change. Calling a String method may build a different String. The original object stays as it was. A variable that referred to the original keeps that reference until the program assigns a new value to the variable.
String mist = "cedar";
String keep = mist;
mist.substring(0, 3);
System.out.print(mist);
mist = mist.substring(0, 3);
System.out.print("," + mist + "," + keep);
The lone call mist.substring(0, 3) builds "ced" and discards it. mist still refers to "cedar", so the first print is cedar. After mist = mist.substring(0, 3), mist refers to the new string "ced". keep still refers to "cedar" because keep was never reassigned.
String tip = "fir";
String twin = tip;
tip = tip + "e";
System.out.print(tip + "/" + twin);
Concatenation also creates a new String. tip becomes "fire". twin still refers to "fir". The characters inside the original "fir" object never switch to "fire".
Key points
- String objects are immutable; their characters do not change in place.
- Methods such as substring return a new String.
- A variable changes only when the program assigns a new reference to it.
- A second variable that still points at the old String is unaffected by that assignment.
Calling substring does not change the original string unless you assign the result.
1.15.B
String indexes and methods
Every character in a String has an index that starts at 0 for the first character. length() returns how many characters the string holds. For a string of length n, valid character indexes run from 0 through n - 1.
substring(from, to) returns a new String made of the characters from index from through index to - 1. It includes from and excludes to. substring(i, i) is legal and yields the empty string. The one-argument form substring(from) runs from that index through the end of the string.
Indexes start at 0. substring(1, 4) keeps characters at 1, 2, and 3 and stops before 4.
String ridge = "glacier";
System.out.print(ridge.length());
System.out.print("," + ridge.substring(1, 4));
System.out.print("," + "[" + ridge.substring(2, 2) + "]");
System.out.print("," + ridge.indexOf("ci"));
System.out.print("," + ridge.indexOf("zz"));
That sequence prints 7,lac,[],2,-1. length is 7. substring(1, 4) is lac. substring(2, 2) is empty. indexOf returns the first starting index of a match, or -1 when the target is not found.
equals compares the characters of two strings and is case-sensitive: "coral".equals("coral") is true, but "coral".equals("Coral") is false. compareTo also uses case-sensitive dictionary order. It reports only the sign of the order: negative when the receiver comes before the argument, positive when it comes after, and 0 when the strings are identical. Do not treat a particular nonzero magnitude as a documented distance.
System.out.print("coral".equals("Coral"));
System.out.print("," + ("bay".compareTo("cove") < 0));
System.out.print("," + ("cove".compareTo("bay") > 0));
System.out.print("," + ("bay".compareTo("bay") == 0));
The prints are false,true,true,true. bay comes before cove, so bay.compareTo("cove") is negative. cove.compareTo("bay") is positive. Equal strings yield 0.
Key points
- Character indexes start at 0; length() is the character count.
- substring(from, to) includes from and excludes to; substring(i, i) is empty.
- indexOf returns the first starting index of a match, or -1 if none.
- equals compares contents and is case-sensitive.
- compareTo is case-sensitive dictionary order; use the sign, not a fixed magnitude.