Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

String — immutable text

String represents immutable text in Java. Learn content versus reference comparison, value conversion, regular expressions, and explicit byte encoding and decoding.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

String text = "Java";
String copy = new String(text);
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
String decoded = new String(bytes, StandardCharsets.UTF_8);

String is a reference type representing immutable text. Indices and length() refer to UTF-16 code units; one code point can occupy two positions. String(String) creates a separate object with the same content, usually unnecessarily. String.intern() returns the pooled representative of the content; literals are interned.

equals compares content, whereas == compares references. String.contentEquals also compares content with a CharSequence, such as StringBuilder. String.compareToIgnoreCase orders text lexicographically without distinguishing case: a negative result, zero, or a positive result means earlier, equal, or later text, respectively. It ignores locale; use Collator for linguistic ordering.

String.valueOf converts values to text; its Object overload returns "null" for a null argument. String.concat appends text without changing the receiver — retain the result.

String.matches matches a regular expression against the entire text. String.replaceAll replaces all matches; $ and \ have special meaning in the replacement. Use replace for literal replacement, or Matcher.quoteReplacement for a literal replacement value in a regex operation. An invalid regex causes PatternSyntaxException.

String.getBytes(Charset) encodes text, and String(byte[],Charset) decodes bytes. Explicitly specify the same charset, such as StandardCharsets.UTF_8; String.getBytes() uses the default charset. The Charset overloads replace malformed or unmappable data; configure CharsetEncoder or CharsetDecoder for strict validation.

Examples

import java.nio.charset.StandardCharsets;

public class StringDemo {
    public static void main(String[] args) {
        String text = "Java";
        String copy = new String(text);
        System.out.println(text.equals(copy) + ":" + (text == copy));
        System.out.println(copy.intern() == text);
        System.out.println(text.compareToIgnoreCase("JAVA"));
        System.out.println(text.contentEquals(new StringBuilder("Java")));
        String label = text.concat(String.valueOf(21));
        System.out.println(label.matches("Java[0-9]+"));
        System.out.println(label.replaceAll("[0-9]+", "SE"));
        byte[] bytes = label.getBytes(StandardCharsets.UTF_8);
        System.out.println(new String(bytes, StandardCharsets.UTF_8));
    }
}

Result

true:false
true
0
true
true
JavaSE
Java21

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources