Skip to content
Tutorials Center
My learning

Topics

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>.

Java
String s = "Hello";
String t = new String("Hello");   // rarely needed
String u = s + ", world";         // new String

Try it

Java Creating and inspecting strings

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.

Java Building, splitting and joining text

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

Java
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). Use codePointCount() or codePoints() 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 like a + b + c is fine; the compiler optimizes it.
  • null is not an empty string. Calling any method on a null reference throws NullPointerException; "" + null yields "null".
  • Case and locale. toUpperCase() and toLowerCase() without arguments use the default locale; pass Locale.ROOT for 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 a Charset when 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() and stripIndent() (Java 15), indent() (Java 12).