Skip to content

Chapter 8 of 37

Strings and String Handling

Use String, StringBuilder, StringBuffer, Unicode-aware operations, and formatting.

36 minutes 10 quick checksBy Subha Prasad
Lesson 8 of 37Course navigation

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.

Which statement best describes String?
Which Java term matches this description: An immutable sequence of UTF-16 code units.
Which statement best describes string pool?
Which Java term matches this description: A runtime area that reuses interned string literals.
Which statement best describes StringBuilder?
Which Java term matches this description: A mutable, unsynchronized character buffer for local construction.
Which statement best describes StringBuffer?
Which Java term matches this description: A synchronized mutable character buffer retained for compatibility.
Which statement best describes equals?
Which Java term matches this description: The method used for String content equality.

0 of 10 checks passed

Your progress is saved on this device.