String is the object you will use more than any other. A String is a sequence of characters with positions (indices) numbered from 0. Strings are immutable: every โchangeโ actually produces a brand-new String.
Indices start at 0
The first character is at index 0, and the last is at index length() - 1. Get this off by one and half the String questions go wrong.
[Diagram โ see the figure in the print workbook.]
The String methods on the exam
| Method | Returns | Example on "COMPUTER" |
|---|---|---|
| length() | number of characters | 8 |
| substring(i) | from index i to the end | substring(3) โ "PUTER" |
| substring(i, j) | from i up to but NOT including j | substring(3, 6) โ "PUT" |
| indexOf(str) | index of first match, or โ1 | indexOf("PUT") โ 3 |
| equals(str) | true if same characters | compare contents |
| compareTo(str) | <0, 0, or >0 by dictionary order | ordering |
Know exactly what each returns โ especially the half-open substring(i, j).
Concatenation builds Strings
The + operator joins Strings. If either side of + is a String, the other side is converted to text. Evaluation is left to right, which leads to a classic trap.
System.out.println("Sum: " + 5 + 5);
System.out.println(5 + 5 + " total");
Output:
Sum: 55
10 total
Worked example 1
Length and last index.
String s = "GALAXY";
System.out.println(s.length());
Output:
6
6 characters, so valid indices are 0..5. The last character 'Y' is at index 5 = length() - 1.
Worked example 2
Both forms of substring.
String s = "COMPUTER";
System.out.println(s.substring(3));
System.out.println(s.substring(3, 6));
Output:
PUTER
PUT
substring(3) takes index 3 to the end. substring(3, 6) takes indices 3, 4, 5 โ it stops before 6. Its length is 6 - 3 = 3.
Worked example 3
indexOf, including the not-found case.
String s = "banana";
System.out.println(s.indexOf("a"));
System.out.println(s.indexOf("z"));
Output:
1
-1
The first โaโ is at index 1 (b=0, a=1). โzโ does not occur, so indexOf returns โ1.
Worked example 4
The concatenation order trap.
int a = 3, b = 4;
System.out.println("" + a + b);
System.out.println(a + b + "");
Output:
34
7
Left to right. With the empty String first, each number is glued as text โ โ34โ. With it last, a + b adds first (7), then becomes text.
Worked example 5
== vs. equals.
String x = new String("hi");
String y = new String("hi");
System.out.println(x == y);
System.out.println(x.equals(y));
Output:
false
true
x and y are two different objects, so == (same object?) is false. Their contents match, so equals is true. Compare text with equals.
Common trap: Use equals, not == , for Strings
== on objects compares references (are these the same object?), not contents. Two Strings with identical text can live at different addresses, so == may be false even when the text matches. Always compare String contents with s1.equals(s2).
Hack: substring(i, j) โ length is j minus i
Two facts kill most substring mistakes: it starts at i and stops just before j, and the result's length is exactly j - i. So substring(2, 5) always returns 3 characters. If j equals length(), you go to the end โ and substring(i, i) is the empty String.
For comparisons: contents โ equals, identity โ ==. On the exam, comparing Strings with == is almost always the wrong answer.