Java Reference Β· java.lang
The String class
Reference for java.lang.String: immutable text, the string pool, common methods, equality, formatting, text blocks and performance notes.
String represents a sequence of UTF-16 code units and is one of the most used classes in Java. Strings are immutable: every method that appears to change a string returns a new one, and the original is never altered. That makes them safe to share between threads and to use as HashMap keys.
String literals like "hello" are instances of String created by the compiler and shared through the string pool, so identical literals refer to the same object. A String is also a CharSequence and a Comparable<String>.
String s = "Hello"; String t = new String("Hello"); // rarely needed String u = s + ", world"; // new String
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class String implements java.io.Serializable, Comparable<String>, CharSequence
| Constructor / member | Description |
|---|---|
String(), String(String s) |
Empty string, copy of another string. Copying is almost never needed. |
String(char[] chars), String(byte[] bytes, Charset cs) |
Build from characters or from encoded bytes. |
int length() |
Number of UTF-16 code units. See length(). |
char charAt(int i) |
Code unit at an index. See charAt(). |
String substring(int from, int to) |
Part of the string. See substring(). |
int indexOf(String s) |
First position or -1. See indexOf(). |
String[] split(String regex) |
Split by a regular expression. See split(). |
String replace(CharSequence a, CharSequence b) |
Replace all literal matches. See replace(). |
boolean equals(Object o) |
Content comparison. See equals(). |
static String valueOf(...), format(...), join(...) |
Static factories. |
Usage notes
- Compare with
equals(), never==.==compares references, which happen to match for literals but not for strings built at run time. - Length counts code units, not characters. Emoji and other characters outside the Basic Multilingual Plane use two
chars (a surrogate pair). UsecodePointCount()orcodePoints()for real characters. - Concatenation in loops is slow. Each
+creates a new string, so building text in a loop is quadratic. Use StringBuilder. A single expression likea + b + cis fine; the compiler optimizes it. nullis not an empty string. Calling any method on anullreference throwsNullPointerException;"" + nullyields"null".- Case and locale.
toUpperCase()andtoLowerCase()without arguments use the default locale; passLocale.ROOTfor machine-readable text. - Switch works on strings (since Java 7) using
equals()semantics. - Encoding. Converting to bytes with
getBytes()uses the platform default charset in old versions; since Java 18 the default is UTF-8. Always pass aCharsetwhen it matters. - Memory. Since Java 9 Latin-1 text is stored in one byte per character internally (compact strings), transparent to your code.
- Text blocks (
""", final since Java 15) are ordinary strings with incidental indentation removed. - Many newer methods:
isBlank(),strip(),lines(),repeat()(Java 11),formatted()andstripIndent()(Java 15),indent()(Java 12).