String (Java)

A String in Java is a sequence of characters used to store text, written in double quotes. Strings are objects and are immutable, meaning their contents can't be changed after they're created.

Java

Because Strings are immutable, methods like .toUpperCase() or .replace() return a new String rather than modifying the original. Use .equals() to compare String contents, not == which compares references.

Useful methods include .length(), .charAt(), .substring(), .toUpperCase(), and .contains().

Examples

Java

String greeting = "hello";
System.out.println(greeting.toUpperCase());
System.out.println(greeting.length());
System.out.println(greeting.charAt(0));

Output:

HELLO
5
h

Learn it in a lesson

See also

Back to the Dictionary