java编译器对字符串“+”连接的优化

下面一段源代码:

使用JD-GUI反编译如下:

PS: 它在干什么,为了方便看么?

使用IntelliJ IDEA的内置反编译工具:

PS: 原样反编译

使用jdk自带工具javap反编译字节码如下:

可以看到 + 连接符被StringBuilder代替

换用Luyten看下:

java

字节码

 

规范是这么说的:http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

发表评论

电子邮件地址不会被公开。 必填项已用*标注