String Concatenation and Autoboxing in Java

浪子不回头ぞ 提交于 2021-01-27 05:24:36

问题


When you concatenate a String with a primitive such as int, does it autobox the value first.

ex.

String string = "Four" + 4;

How does it convert the value to a string in Java?


回答1:


To see what the Java compiler produces it is always useful to use javap -c to show the actual bytecode produced:

For example the following Java code:

String s1 = "Four" + 4;
int i = 4;
String s2 = "Four" + i;

would produce the following bytecode:

   0:   ldc     #2; //String Four4
   2:   astore_1
   3:   iconst_4
   4:   istore_2
   5:   new     #3; //class java/lang/StringBuilder
   8:   dup
   9:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   12:  ldc     #5; //String Four
   14:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
   17:  iload_2
   18:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(I)Ljava/lan
g/StringBuilder;
   21:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/la
ng/String;
   24:  astore_3
   25:  return

From this we can see:

  • In the case of "Four" + 4, the Java compiler (I was using JDK 6) was clever enough to deduce that this is a constant, so there is no computational effort at runtime, as the string is concatenated at compile time
  • In the case of "Four" + i, the equivalent code is new StringBuilder().append("Four").append(i).toString()
  • Autoboxing is not involved here as there is an StringBuilder.append(int) method which according to the docs is using String.valueOf(int) to create the string representation of the integer.



回答2:


The java compiler actually creates a StringBuilder1 and invokes the append() method. It can be seen in the byte-code:

22  invokespecial java.lang.StringBuilder(java.lang.String) [40]
...   
29  invokevirtual java.lang.StringBuilder.append(int) : java.lang.StringBuilder [47]
32  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [51]

Nevertheless, the behavior is identical to boxing and then invoking toString(): "Four" + new Integer(4).toString() - which I believe what the language designers had in mind.


(1) To be exact, the compiler is already concatting the string literal and int literal to a single string literal "Four4". You can see it in the byte code in the following line in byte-code:

 0  ldc <String "Four4"> [19]



回答3:


According to http://jcp.org/aboutJava/communityprocess/jsr/tiger/autoboxing.html, autoboxing is done on the primitive type whenever a reference type is needed(such as the Integer class in this case)

So the int will be converted into an Integer then that integer objects toString() method is called and its result is appended to the preceding string.



来源:https://stackoverflow.com/questions/12445064/string-concatenation-and-autoboxing-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!