Unravel โบ Java concepts โบ Strings and text
Strings: immutability, equality, common methods
A String is an immutable piece of text: no method ever changes it, every "change" returns a new String.
At a glance
A printed label.
- 1Common methods: length(), charAt(i), substring(from, to), indexOf("x"), contains, startsWith, replace, trim/strip, split, toUpperCase, isEmpty and isBlank (true for only spaces)
- 2== asks "is it the same object?", equals asks "is the text the same?"String literals are shared through a pool, so "a" == "a" can be true while new String("a") == "a" is false. Always use equals; for a possible null write "yes".equals(input).
- 3Positions start at 0 and ranges are half open: "hello".substring(1, 3) is "el"
- 4Gluing many pieces in a loop with + creates a new String every timeUse a StringBuilder for that.
- split takes a regular expression, so "a.b".split(".") returns nothing useful: the dot matches every character
- Case-insensitive comparison: equalsIgnoreCase
- A String holds a byte[] plus a coder flag (compact strings: one byte per character when all characters are Latin-1, two otherwise)
public class Names { static String initials(String full) { if (full == null || full.isBlank()) return ""; StringBuilder sb = new StringBuilder(); for (String part : full.strip().split("\\s+")) sb.append(part.charAt(0)); return sb.toString().toUpperCase(); } static boolean sameUser(String a, String b) { return a.equalsIgnoreCase(b.substring(1)); }}- line 2 uses String, the core of Strings: immutability, equality, common methods
- line 3 uses isBlank, the core of Strings: immutability, equality, common methods
- line 4 uses String, the core of Strings: immutability, equality, common methods
- line 5 uses String, the core of Strings: immutability, equality, common methods
- line 6 uses String, the core of Strings: immutability, equality, common methods
- line 8 uses String, the core of Strings: immutability, equality, common methods
Read the full explanation (about 1 min)
A String is an immutable piece of text: no method ever changes it, every "change" returns a new String. Compare strings with equals, not ==.
A mental model
A printed label. To change the wording you print a new label; the old one stays as it was, which is why many parts of a program can safely share the same label.
How it works
Common methods: length(), charAt(i), substring(from, to), indexOf("x"), contains, startsWith, replace, trim/strip, split, toUpperCase, isEmpty and isBlank (true for only spaces).
== asks "is it the same object?", equals asks "is the text the same?". String literals are shared through a pool, so "a" == "a" can be true while new String("a") == "a" is false. Always use equals; for a possible null write "yes".equals(input).
Positions start at 0 and ranges are half open: "hello".substring(1, 3) is "el".
Gluing many pieces in a loop with + creates a new String every time. Use a StringBuilder for that.
Going deeper
split takes a regular expression, so "a.b".split(".") returns nothing useful: the dot matches every character. Use split("\\.") or Pattern.quote.
Case-insensitive comparison: equalsIgnoreCase. For user-visible sorting or case changes in different languages, specify a Locale (toLowerCase(Locale.ROOT)): the Turkish dotless i breaks naive code.
Strings are used as map keys because their hashCode is cached after first use and they cannot change under the map. Keep passwords in char[] instead if you want to wipe them.
Under the hood
A String holds a byte[] plus a coder flag (compact strings: one byte per character when all characters are Latin-1, two otherwise). intern() moves a string into the JVM-wide pool; literals are interned automatically, and substring copies the data in modern Java.
Common mistakes
- Comparing with
==. - Expecting
s.toUpperCase();alone to changes. - Using
split(".")orsplit("|")without escaping. - Concatenating in a big loop.
How Unravel recognises it
When you paste code, Unravel looks for these signs of strings: immutability, equality, common methods. Spotting them yourself is a good habit when you read unfamiliar code:
- Method calls:
charAt()substring()indexOf()lastIndexOf()toCharArray()startsWith()endsWith()equalsIgnoreCase()strip()isBlank()intern()replace()chars()repeat()and 2 more - Created with new:
String
Part of a learning path
Related concepts
Builds on this
Frequently asked questions
What is Strings: immutability, equality, common methods?
A String is an immutable piece of text: no method ever changes it, every "change" returns a new String. Compare strings with equals, not ==.
What are the most common mistakes with Strings: immutability, equality, common methods?
Comparing with ==. Expecting s.toUpperCase(); alone to change s. Using split(".") or split("|") without escaping. Concatenating in a big loop.
How can I see Strings: immutability, equality, common methods in real code?
Paste your own Java into Unravel and it will point out where strings: immutability, equality, common methods appears, explain how it works at the detail level you choose, and draw the flow of the code around it.