Lesson content
Read, practise, then check your understanding
String is immutable. Literals may be interned in a pool, but identity with == is not content equality; use equals, equalsIgnoreCase, or a comparator. Concatenation creates a new logical value and compilers may optimize simple expressions.
Building and inspecting text
String name = "Java";
boolean match = name.equals("Java");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 3; i++) {
builder.append(i).append(':').append(name).append('\n');
}
String report = builder.toString();
Use StringBuilder for repeated mutation in one thread. StringBuffer offers synchronized legacy operations but rarely solves higher-level concurrency. substring, indexOf, replace, split, formatted strings, and regex APIs cover common handling. A Java char is one UTF-16 code unit, not necessarily a complete Unicode character; use code-point methods when supplementary characters matter. Validate locale and charset explicitly for external text.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.