When should we use intern method of String on String literals

后端 未结 14 795
生来不讨喜
生来不讨喜 2020-11-22 08:11

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string

14条回答
  •  礼貌的吻别
    2020-11-22 08:43

    I want to add my 2 cents on using == with interned strings.

    The first thing String.equals does is this==object.

    So although there is some miniscule performance gain ( you are not calling a method), from the maintainer point of view using == is a nightmare, because some interned strings have a tendency to become non-interned.

    So I suggest not to rely on special case of == for interned strings, but always use equals as Gosling intended.

    EDIT: interned becoming non-interned:

    V1.0
    public class MyClass
    {
      private String reference_val;
    
      ...
    
      private boolean hasReferenceVal ( final String[] strings )
      {
        for ( String s : strings )
        {
          if ( s == reference_val )
          {
            return true;
          }
        }
    
        return false;
      }
    
      private void makeCall ( )
      {
         final String[] interned_strings =  { ... init with interned values ... };
    
         if ( hasReference( interned_strings ) )
         {
            ...
         }
      }
    }
    

    In version 2.0 maintainer decided to make hasReferenceVal public, without going into much detail that it expects an array of interned strings.

    V2.0
    public class MyClass
    {
      private String reference_val;
    
      ...
    
      public boolean hasReferenceVal ( final String[] strings )
      {
        for ( String s : strings )
        {
          if ( s == reference_val )
          {
            return true;
          }
        }
    
        return false;
      }
    
      private void makeCall ( )
      {
         final String[] interned_strings =  { ... init with interned values ... };
    
         if ( hasReference( interned_strings ) )
         {
            ...
         }
      }
    }
    

    Now you have a bug, that may be very hard to find, because in majority of cases array contains literal values, and sometimes a non-literal string is used. If equals were used instead of == then hasReferenceVal would have still continue to work. Once again, performance gain is miniscule, but maintenance cost is high.

提交回复
热议问题