Ch 11 · Strings — Working with Text · Java
Topic 11 of 13 in Java — Complete Tutorial (Beginner) — 2 lessons.
String Basics & Methods
A String holds text — names, messages, anything human-readable. Strings come with many built-in methods to inspect and transform text, which you call with a dot. A key rule: Strings are immutable, meaning a method like toUpperCase() does not change the original — it returns a brand-new String. To keep the change, store it in a variable. Get the number of characters with .length() (with brackets, unlike arrays).
Comparing Strings & Formatting
To compare two Strings for equal text, always use .equals(...), never ==. The == operator checks whether they are the exact same object in memory, which is usually false even when the text matches — a classic beginner bug. For building formatted text, String.format(...) and printf use placeholders like %d (whole number), %.2f (decimal to 2 places), and %s (string).