In programming there is a lot of "wisdom", "rumours", "magic" or "superstition" going around.
As @RealSkepic points out, Before Java 7u4, String.substring would use a portion of the original string rather than copy that portion. While this improved performance in many cases, it could lead to memory leaks. Using intern()
was one way to avoid this, though it could create it's own memory clean up problems which is not ideal. Using new String(oldString)
was another approach, but you shouldn't need to do that now.
People often try things for "performance reasons" but don't know how to test it, or just don't check it actually helps. I do this from time to time, even though I know to avoid it, because it is far too often incorrect, or just makes the code confusing.
Most likely the author found a situation, or heard some one saved a lot of memory by using String.intern()
and in specific cases, it can do this, but it's not like fairy dust where you sprinkle a bit of performance magic and everything is better. Most of these obscure tricks to optimise code only work in very specific use cases.
A similar example is when people using locks or thread safe collections in multi-threading. Sprinkle enough of this around and the program can appear to stop an error, but you haven't really fixed the problem, just made it harder to find when something incidental changes and your bug shows itself again.