欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDK5对字符串+操作进行的优化 Java 

程序员文章站 2024-02-04 18:42:52
...
zli.ray 写道
超级潜水艇 写道
第七点已经在JDK6的时候由编译器进行了优化,所以在一般的编写过程中是无需使用StringBuffer或StringBuilder的。编译器会在编译时自动将+的操作转变成StringBuilder的操作。。由我来看使用+操作比使用StringBuilder更加易懂明了(既然编译器已经为你做了优化)。但是有一点要注意。在一个循环中还是得自己写StringBuilder。因为这时编译器是不会给你进行优化的

这个挺好,方便很多。
但没看到官方有说明...

 

这个容易,JDK5编译器就会优化字符串+操作了。不过不要在loop里面使用+(每次loop都会创建一个StringBulider):

 /**
    * @version $Id: StringConcatTest.java Exp $              
   */

public class StringConcatTest {
        //普通字符串相加,JDK5 ↑编译器会自动转为StringBulider处理
        public static String testConcat(String begin){
                return begin+"5"+"2"+"1";
        }
        // 不要循环里相加,每次循环创建一个StringBulider
        public static String testConcatInLoop(String begin){
                for(int i=10;i>0;i--){
                        begin+=""+i;
                }
                return begin;
        }
}

 然后用javap看一下字节码,一目了然:

javap -verbose StringConcatTest > c:\cyy.txt

 下面是文件内容,有点类汇编:

public static java.lang.String testConcat(java.lang.String);
  Code:
   Stack=3, Locals=1, Args_size=1
   0:	new	#16; //class java/lang/StringBuilder
   3:	dup
   4:	aload_0
   5:	invokestatic	#18; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
   8:	invokespecial	#24; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   11:	ldc	#27; //String 5
   13:	invokevirtual	#29; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   16:	ldc	#33; //String 2
   18:	invokevirtual	#29; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   21:	ldc	#35; //String 1
   23:	invokevirtual	#29; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   26:	invokevirtual	#37; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   29:	areturn
  LineNumberTable: 
   line 18: 0

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      30      0    begin       Ljava/lang/String;


public static java.lang.String testConcatInLoop(java.lang.String);
  Code:
   Stack=3, Locals=2, Args_size=1
   0:	bipush	10
   2:	istore_1
   3:	goto	28
   6:	new	#16; //class java/lang/StringBuilder
   9:	dup
   10:	aload_0
   11:	invokestatic	#18; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
   14:	invokespecial	#24; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   17:	iload_1
   18:	invokevirtual	#44; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   21:	invokevirtual	#37; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   24:	astore_0
   25:	iinc	1, -1
   28:	iload_1
   29:	ifgt	6  //循环内调用 + 操作,又跳至第6行创建了一个StringBulider
   32:	aload_0
   33:	areturn
  LineNumberTable: 
   line 22: 0
   line 23: 6
   line 22: 25
   line 25: 32

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      34      0    begin       Ljava/lang/String;
   3      29      1    i       I


}
相关标签: Java