Thursday, June 29, 2023

Horror Code: Interning String Literals

 This article was originally posted on JRoller on June 29, 2012

I found this kind of code in our application:

  public static final String CONSTANT_STRING = "Some value".intern();

Interning a String means storing it into a common pool of Strings managed by the JVM, so that if I already used this String somewhere, I will only get back its reference. It may be a good idea sometimes, but you have to know that all String literals are already interned by the JVM. As useless as calling toString() on a String.

No comments:

Post a Comment