I understand the basic idea of java's String interning, but I'm trying to figure out which situations it happens in, and which I would need to do my own flyweighting.
Somewhat related:
- Java Strings: “String s = new String(”silly“);”
- The best alternative for String flyweight implementation in Java never quite got answered
Together they tell me that String s = "foo"
is good and String s = new String("foo")
is bad but there's no mention of any other situations.
In particular, if I parse a file (say a csv) that has a lot of repeated values, will Java's string interning cover me or do I need to do something myself? I've gotten conflicting advice about whether or not String interning applies here in my other question
HashMap alternatives for memory-efficient data storage
The full answer came in several fragments, so I'll sum up here:
By default, java only interns strings that are known at compile-time. String.intern(String)
can be used at runtime, but it doesn't perform very well, so it's only appropriate for smaller numbers of String
s that you're sure will be repeated a lot. For larger sets of Strings it's Guava to the rescue (see ColinD's answer).