upvote
> Wait, why would interned immutable strings require more instructions when doing regular string access

Sorry, I wasn’t precise. Accessing them won’t take more instructions, but setting them up does.

> Or do you refer to the extra rolling-hash pass needed when concatenating two strings to verify if it would result in an already-interned one?

I don’t think the JVM does that.

reply
> Wait, why would interned immutable strings require more instructions when doing regular string access?

Java automatically interns static strings (e.g. from class files), but does not automatically intern dynamically-allocated strings, e.g. new String(charArray)

If you want it interned, you have to intentionally call e.g. new String(...).intern(). If you do this on every string you work with, you can then reliably use reference equality instead of value equality, e.g. given char[] abc = {'a','b','c'}; then new String(abc) != new String(abc) != "abc" but new String(abc).intern() == new String(abc).intern() == "abc"

But if you're interning every string, you're doing extra work to maintain that string pool, and adding extra pressure on the GC, and potentially you'll be re-interning strings a lot depending on how many times they end up no longer referenced by the time GC runs.

reply