String中的秘密

作者: chenmingang | 来源:发表于2017-02-22 14:37 被阅读109次

    字符串相加

    字符串相加在Java中使用非常频繁,通常情况下使用"+"就可以方便快捷的完成目标,但是你可能听过"+"在有些时候效率不高,需要用StringBuilder代替.但是这种说法的背后原因是什么呢,哪种情况下适用呢?下面结合Jdk命令Javap解释这俩问题.

    一个普通的字符串相加:
    public class StringTest {
        public static void main(String[] args){
          String a = "a";
          String b = "b";
          String c = "c";
          String d = a + b + c;
          System.out.println(d);
        }
    }
    

    使用javap查看编译后的字节码:

    zeal@zeal-ThinkPad:~/tobenew$ javap -c StringTest
    Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
    Compiled from "StringTest.java"
    public class StringTest {
      public StringTest();
        Code:
           0: aload_0       
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return        
      public static void main(java.lang.String[]);
        Code:
           0: ldc           #2                  // String a
           2: astore_1      
           3: ldc           #3                  // String b
           5: astore_2      
           6: ldc           #4                  // String c
           8: astore_3      
           9: new           #5                  // class java/lang/StringBuilder
          12: dup           
          13: invokespecial #6                  // Method java/lang/StringBuilder."<init>":()V
          16: aload_1       
          17: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          20: aload_2       
          21: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          24: aload_3       
          25: invokevirtual #7                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          28: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
          31: astore        4
          33: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
          36: aload         4
          38: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          41: return        
    }
    

    我们并不讨论具体的指令含义,简单来看"+"在编译中被StringBuilder替换,所以说"+"只是一个语法上的简化,最后通过编译器编译成了StringBuilder的append实现.可以发现在这里"+"和手写append其实是一样的,"+"更易读些.

    循环中的字符串相加:
    public class StringTest {
       public static void main(String[] args){
         final String a = "a";
         String b = "b";
         for(int i=0; i<5; i++) {
            b = b + a;
         }
         System.out.println(b);
       }
    }
    

    使用javap查看编译后的字节码:

    zeal@zeal-ThinkPad:~/tobenew$ javap -c StringTest
    Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
    Compiled from "StringTest.java"
    public class StringTest {
      public StringTest();
        Code:
           0: aload_0       
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return        
    
      public static void main(java.lang.String[]);
        Code:
           0: ldc           #2                  // String b
           2: astore_2      
           3: iconst_0      
           4: istore_3      
           5: iload_3       
           6: iconst_5      
           7: if_icmpge     36
          10: new           #3                  // class java/lang/StringBuilder
          13: dup           
          14: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
          17: aload_2       
          18: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          21: ldc           #6                  // String a
          23: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
          26: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
          29: astore_2      
          30: iinc          3, 1
          33: goto          5
          36: getstatic     #8                  // Field java/lang/System.out:Ljava/io/PrintStream;
          39: aload_2       
          40: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          43: return        
    }
    

    仔细看可以发现StringBuilder对象创建被放在了循环里面(goto语句),所以如果循环次数特别多就不适合用"+"了,要不然会创建大量的StringBuilder对象.正确的用法是这样:

    public class StringTest {
       public static void main(String[] args){
         String a = "a";
         StringBuilder builder = new StringBuilder("b");
         for(int i=0; i<5; i++) {
            builder.append(a);
         }
         System.out.println(builder.toString());
       }
    }
    
    final字符串的相加:
    public class StringTest {
        public static void main(String[] args){
          final String a = "a";
          final String b = "b";
          final String c = "c";
          String d = a + b + c;
          System.out.println(d);
        }
    }
    

    使用javap查看编译后的字节码:

    zeal@zeal-ThinkPad:~/tobenew$ javap -c StringTest
    Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
    Compiled from "StringTest.java"
    public class StringTest {
      public StringTest();
        Code:
           0: aload_0       
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return        
      public static void main(java.lang.String[]);
        Code:
           0: ldc           #2                  // String abc
           2: astore        4
           4: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
           7: aload         4
           9: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
          12: return        
    }
    

    我们发现根本没有StringBuilder对象被创建,因为a,b,c值不会改变,所以d是一个确定值.Java在编译期就进行了优化.

    字符串常量池的位置

    常量池的位置在Jdk1.6之前(包括1.6)属于永久区的一部分,而在1.7之后(包括1.7)被移到了堆中管理.

    import java.util.List;
    import java.util.ArrayList;
    public class StringTest {
       public static void main(String[] args){
            List<String> strList = new ArrayList<String>();
        int i=0;
        while(true){
            strList.add((i+"").intern());
            i++;
        }
       }
    }
    
    zeal@zeal-pc:~$ ./soft/jdk/jdk1.6.0_45/bin/java -Xmx10m -XX:MaxPermSize=5m -XX:-UseGCOverheadLimit StringTest
    Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
        at java.lang.String.intern(Native Method)
        at StringTest.main(StringTest.java:8)
    
    zeal@zeal-pc:~$ ./soft/jdk/jdk1.7.0_65/bin/java -Xmx10m -XX:MaxPermSize=5m -XX:-UseGCOverheadLimit StringTest
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at StringTest.main(StringTest.java:8)
    

    字符串比较

    这个在整理的时候遇到一个奇葩情况解释不了,请大神回复一下
    首先来看一波符合预期的例子:

    //In Jdk1.7
    public class StringTest {
       public static void main(String[] args){
            String str1 = new String("world");
            String str2 = str1.intern();
            String str3 = "world";
    
            System.out.println(str1 == str2);  //false
            System.out.println(str2 == str3);  //true
       }
    }
    
    //In Jdk1.7
    public class StringTest {
       public static void main(String[] args){
            String str0 = "world";
            String str1 = new String("world");
            String str2 = str1.intern();
            String str3 = "world";
    
            System.out.println(str0 == str1);  //false
            System.out.println(str0 == str2);  //true
            System.out.println(str1 == str2);  //false
            System.out.println(str2 == str3);  //true
            System.out.println(str0 == str3);  //true
       }
    }
    

    看一下intern的源码注释:(Jdk1.6和Jdk1.7是完全一样的)

    When the intern method is invoked, if the pool already contains a
    string equal to this <code>String</code> object as determined by
    the {@link #equals(Object)} method, then the string from the pool is
    returned. Otherwise, this <code>String</code> object is added to the
    pool and a reference to this <code>String</code> object is returned.

    如果常量池中存在和当前对象equals相等的对象则返回常量池中的对象引用,否则将当前对象(应该是当前对象的拷贝)加入常量池并返回引用.
    从这里可以看出,"world"这种创建字符串的方式会被加入到常量池,而new对象的方式是不会添加到常量池或者从常量池中获取的.

    不符合预期的情况

    //In Jdk1.7
    public class StringTest {
       public static void main(String[] args){
         String str1=new String("hell")+new String("o");
         String str2 = str1.intern();
         String str3 = "hello";
         String str4 = new String("hello");
    
         System.out.println(str1==str2);
         System.out.println(str2==str3);
         System.out.println(str1==str3);
         System.out.println(str3==str4);
       }
    }
    
    zeal@zeal-pc:~$ java StringTest
    true
    true
    true
    false
    

    根据前文可以看到str1是由StringBuilder的toString方法构造出来的,查看源码可以发现toString其实是new了一个新的String对象出来.按照之前理解str1==str2应为false.

    madan坑好深,谁来解释一下

    public class StringTest {
       public static void main(String[] args){
            String str1 = new String("hell")+new String("o");
            String str2 = str1.intern();
    
            String str3 = new String("java");
            String str4 = str3.intern();
    
            String str5 = new String("world");
            String str6 = str5.intern();
    
            String str7 = "cat";
            String str8 = str7.intern();
    
            String str9 = new StringBuffer("hello").append("world").toString();
            String str10 = str9.intern();
    
            String str11 = new StringBuilder("hellojava").toString();
            String str12 = str11.intern();
    
            String str13=new StringBuilder("hello").append("2").toString();
            String str14 = "hello2";
            String str15=str13.intern();
        
                                               //Jdk1.7   //Jdk1.6
            System.out.println(str1 == str2);  //true?    //false
            System.out.println(str3 == str4);  //false    //false
            System.out.println(str5 == str6);  //false    //false
            System.out.println(str7 == str8);  //true     //true
            System.out.println(str9 == str10); //true?    //false
            System.out.println(str11 == str12);//false    //false
            System.out.println(str13==str14);  //false    //false
            System.out.println(str13==str15);  //false    //false
            System.out.println(str14==str15);  //true     //true
       }
    }
    

    之后进行了一系列测试,发现append和intern在一起的时候("+"等同于append)在1.7中会发生奇妙的现象.表面来看就好像append存在的时候往常量池中存放的不是String对象的拷贝而是String对象的引用.

    只能把锅给append了.... 谁来解释一下!

    相关文章

      网友评论

      • 9b3e39d69ca6:关于
        String str1=new String("hell")+new String("o");
        String str2 = str1.intern();
        String str3 = "hello";
        String str4 = new String("hello");
        我的理解是 一个变量 一旦调用入池 他自身的地址也会发生改变 ,即str1变量在调用入池后 自身的地址变成了池中的地址

      本文标题:String中的秘密

      本文链接:https://www.haomeiwen.com/subject/lnllwttx.html