Command Palette

Search for a command to run...

Phase 6Intermediate7 of 17 in Core Java

Strings and Text in Depth

Master Java text: the string pool, == vs equals, StringBuilder, every everyday method, text blocks, formatting, regular expressions and Unicode.

Almost every program handles text: names, messages, JSON, log lines, file paths, user input. Topic 1.10 showed you the basics of String. This phase goes underneath: where String objects live in memory, why they can never change, why == sometimes seems to work and then betrays you, and how to build text fast with StringBuilder instead of creating thousands of throwaway objects.

Then you'll get fluent with the tools you'll use every day: the essential String methods (including the Java 11 additions like strip, isBlank, lines and repeat), text blocks for multi-line text (Java 15), String.format and formatted for clean output, and regular expressions for searching, validating and splitting text. The phase ends with characters themselves: char, Unicode code points, surrogate pairs and encodings like UTF-8, which is where most "why is my text garbled?" bugs come from.

Every topic builds on Phase 4 (objects, references, equals/hashCode, immutability). What you learn here comes back in Phase 9 (Strings are the most common HashMap key), Phase 12 (reading and writing text files with the right encoding) and Phase 14 (how the JVM stores and deduplicates Strings).

0/8 · 0%
8 topics ~4 h 33 code blocks & diagrams
Start with the first topic
1
6.1

The String Pool and Immutability

A String object can never change after it's created, which lets the JVM keep one shared copy of each string literal in the string pool. Every method that seems to change a String returns a new one, and intern() lets you put run-time Strings into the pool too.

32 min 4 code practice

2
6.2

== vs equals for Strings

== asks whether two references point to the very same String object; equals asks whether two Strings hold the same characters. Always compare text with equals (or equalsIgnoreCase, compareTo, Objects.equals), because == only looks right when the pool happens to share an object.

29 min 4 code practice

3
6.3

StringBuilder and StringBuffer

StringBuilder is a mutable, growable buffer of characters for building text step by step without creating a new String on every change; call toString() once at the end. StringBuffer is the older, synchronized twin, rarely needed today.

31 min 5 code practice

4
6.4

Essential String Methods

The String class has a method for almost every everyday text job: measuring, searching, slicing, trimming, changing case, replacing, splitting, joining and repeating. Knowing their exact rules (end-exclusive indexes, regex arguments, trim vs strip, how split drops empty pieces) prevents most text bugs.

30 min 4 code practice

5
6.5

Text Blocks

A text block is a multi-line String literal written between triple quotes ("""). It keeps your line breaks, removes the indentation that only exists to line the code up, and needs almost no escaping, which makes JSON, SQL, HTML and test data readable in Java code.

29 min 4 code practice

6
6.6

String Formatting

String.format, printf and formatted build text from a template with placeholders like %s, %d and %.2f, giving you control over width, alignment, padding, decimals and grouping. Pass a Locale for numbers, because the decimal and thousands separators change by country.

28 min 4 code practice

7
6.7

Regular Expressions

A regular expression (regex) is a small pattern language for describing text, such as "three digits, a dash, four digits". Java's java.util.regex package compiles a pattern once (Pattern) and uses it to test, search, extract and replace text (Matcher), and String.matches, split and replaceAll use it too.

31 min 4 code practice

8
6.8

Characters, Unicode and Encodings

Unicode gives every character in every script a number called a code point; a Java char is a 16-bit UTF-16 code unit, so characters beyond U+FFFF (like most emoji) take two chars. When text becomes bytes (files, network), an encoding such as UTF-8 decides the bytes, and using the wrong one on either side garbles the text.

33 min 4 code practice