Inline classes, strings and intern

Brian Goetz brian.goetz at
Fri Dec 20 14:38:53 UTC 2019

In a community as large as Java, any abuse you can think of, will surely be done by someone.  

My from-the-hip opinion on this is not going to be make the existing interning abuses much worse.  Most of the interning abuses came from the early days, before people really got the memo that `==` was a low-level primitive and generally shouldn’t be used for much.  (Fortunately, I see much less use of it these days than in the older days.) 

When I see someone using interning, it’s a red flag, not because it’s intrinsically bad, but because it is almost always misused, generally out of incorrect priorities (e.g., unwarranted performance obsession, syntactic obsession, etc.)  Many of the earlier benefits of interning (memory compaction) now come for free with string deduplication during GC. 

> On Dec 20, 2019, at 5:39 AM, Stephen Colebourne <scolebourne at> wrote:
> If I declare an inline class (examplar syntax):
> _Inline_ class StrPos (String str, int pos) {}
> then the strings are compared with `==` not `.equals()`. Thus:
> a = new StrPos("abba", 2)
> b = new StrPos("abbac".substring(0, 4), 2)
> a == b  -> false
> a.equals(b)  -> true
> If I were the author is such a class, I might not like that. But there
> is a "simple" solution - interning. If I call `String.intern()` on the
> string in the constructor of the inline class then a == b would now be
> true.
> Great! Except that my understanding is that interning is frowned upon
> these days. Obviously the same interning approach could be made to
> work for any type (such as using Guava's `Interners` class), but it
> seems particularly relevant to discuss it wrt `String.intern()`
> because it is built in and has special JVM global behaviour.
> It seems to me that because `String.intern()` exists and is easy to
> use, it could be widely abused to get the desired == behaviour for
> inline classes. Is this a real problem that needs more thought? Or not
> something to worry about?
> Stephen

More information about the valhalla-dev mailing list