Strings
A String is an immutable sequence of characters. Once created, its contents never change. Methods that look like edits return a new String.
Detailed Explanation
Text in Java is String. Literals ("hello") live in the class file and are interned in the string pool, so two literals with the same characters are usually the same object. new String("hello") forces a distinct object and is almost never what you want.
Immutability is the reason String is safe to share as a map key, a lock name, or a message across threads. toUpperCase, substring, and replace allocate another String. The original is untouched. Concatenation with + also allocates. In a loop, that becomes quadratic: each step copies the growing prefix. Use StringBuilder there.
Comparison is a frequent bug. == tests whether two variables point at the same object. Literals may pass that test because of interning. Strings built at runtime usually fail it. Use equals for contents, equalsIgnoreCase when case does not matter, and compareTo for ordering. Objects.equals(a, b) is null-safe.
Internally, modern JDKs store Latin-1 bytes when every character fits in 8 bits, and UTF-16 bytes otherwise (compact strings, Java 9+). length() counts char units, which are 16-bit code units, not Unicode code points. Characters outside the Basic Multilingual Plane, such as many emoji, use two chars. codePointCount and codePoints() are the APIs that see full characters.
You reach for String for text you will compare, store, or print. You reach for char[] or byte[] when you must overwrite a secret after use; a String will sit in memory until GC. You reach for StringBuilder when you build text in pieces. You reach for Formatted/formatted or String.format when you interpolate values.
The cost of String is allocation. Parsing a large file into millions of small strings pressures the garbage collector. Interning huge unique strings can exhaust the heap. Encoding mistakes (new String(bytes) without a Charset) use the platform default and misread files on another machine. Always name StandardCharsets.UTF_8 when you convert bytes.
Code Examples
public class Names {
public static void main(String[] args) {
String a = "Ada";
String b = "Ada";
String c = new String("Ada");
System.out.println(a == b); // prints: true (same pooled literal)
System.out.println(a == c); // prints: false
System.out.println(a.equals(c)); // prints: true
System.out.println(a.toUpperCase()); // prints: ADA
System.out.println(a); // prints: Ada (original unchanged)
}
}import java.nio.charset.StandardCharsets;
public class Bytes {
public static void main(String[] args) {
byte[] raw = "café".getBytes(StandardCharsets.UTF_8);
String again = new String(raw, StandardCharsets.UTF_8);
System.out.println(again); // prints: café
System.out.println(again.length()); // prints: 4 (four Java chars here)
}
}Common Gotchas
`==` is not a content test
it passes for some literals and fails for the same letters built at runtime. Always `equals`.
Concatenating in a loop with `+` copies every prefix
use `StringBuilder` or `String.join`.
`substring` on old JDKs could leak the original array
modern JDKs copy. Do not rely on either behaviour for secrets; use `char[]`.
`length()` counts `char`s, not graphemes
an emoji may report length 2.