Adverts
The String class in Java has a method on it that is, in my experience, little used and probably widely abused â that method is intern. Most developers that I have met only vaguely understand what the intern method does and why it can, at times, be very useful.
The String intern method checks a global cache of String instances (maintained by the String class) for a String that is equal to the given String as determined by .equals. If such a String is found then the globally cached version is returned if itâs not the String is placed in the global String cache and returned. In this way only one instance of a particular String will exist and == (a pointer comparison) can be used to check if two Strings are the same.
Internalizing a String, therefore, provides two useful side effects:
- It saves space by removing duplicate String literals.
- It speeds up String comparisons because == can be used rather then .equals.
At first glance this may not seem to provide all that much benefit and in many systems the difference is probably negligible (which is why I think this method is little used). The .equals method is fast and the memory overhead of a few duplicated Strings is going to be small. If, however, the system in question is dealing with large String based datasets then the savings can be huge. Imagine a million point dataset that containing a String that can be one of only a dozen options. By internalizing that String rather than storing a million String instances the application only needs to store a dozen and a million pointers. Further more whenever you compare that String you can do so with ==.