The compiler converts this phrase ("x =" + x) into a StringBuilder and uses .append (int) to "add" integers to the string.
To go beyond the practical “how Java does it,” I will take Stephen’s advice and give a theoretical assessment. It is clear that each value in concatenation is first converted to String and then concatenated. Zeros are combined as the word "null".
From the Java language specification :
15.18.1.1 String Conversion
Any type can be converted to a String type by converting a string. The value x of the primitive type T is first converted to a control value, as if as an argument to create the corresponding instance of the class expression:
If T is Boolean, then use the new Boolean (x). If T char, then use the new Character (x). If T bytes, short or int, then use the new Integer (x). If T is long, then use the new Long (x). If T is a float, use the new Float (x). If T is double, use the new Double (x). This control value is then converted to a String type by converting the string. Now only reference values ​​should be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by calling the toString of the reference object with no arguments; but if the result of calling the toString method is null, then the string "null" is used instead.
The toString method is defined by the primitive class Object; many classes override it, in particular boolean, character, integer, long, floating, Double and String.
15.18.1.2 Optimizing String Concatenation
An implementation can choose to perform conversion and concatenation in one step to avoid creating and then discarding the intermediate String object. To increase the performance of a repeating concatenation string, the Java compiler can use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluating the expression. For primitive types, an implementation can also optimize the creation of a shell object by converting directly from a primitive type to a string.
The optimized version does not actually make a complete complete String conversion.
This is a good illustration of the optimized version used by the compiler, albeit without primitive conversion, where you can see how the compiler changes things to StringBuilder in the background:
http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/
This java code:
public static void main(String[] args) { String cip = "cip"; String ciop = "ciop"; String plus = cip + ciop; String build = new StringBuilder(cip).append(ciop).toString(); }
Generates this - let's see how two styles of concatenation lead to the same bytecode:
L0 LINENUMBER 23 L0 LDC "cip" ASTORE 1 L1 LINENUMBER 24 L1 LDC "ciop" ASTORE 2 // cip + ciop L2 LINENUMBER 25 L2 NEW java/lang/StringBuilder DUP ALOAD 1 INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String; INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V ALOAD 2 INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String; ASTORE 3 // new StringBuilder(cip).append(ciop).toString() L3 LINENUMBER 26 L3 NEW java/lang/StringBuilder DUP ALOAD 1 INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V ALOAD 2 INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder; INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String; ASTORE 4 L4 LINENUMBER 27 L4 RETURN
The compiler turned "cip + ciop" into "the new StringBuilder (cip) .append (ciop) .toString ()". In other words, “+” is actually a shorthand for the more detailed StringBuilder idiom.