Using this code to simulate your question above:
String s = "asd";
s+=42;
System.out.println(s);
Will result in this byteCote:
Code:
0: ldc #16 // String asd
2: astore_1
3: new #18 // class java/lang/StringBuilder
6: dup
7: aload_1
8: invokestatic #20 // Method java/lang/String.valueOf:(
java/lang/Object;)Ljava/lang/String;
11: invokespecial #26 // Method java/lang/StringBuilder."<
nit>":(Ljava/lang/String;)V
14: bipush 42
16: invokevirtual #29 // Method java/lang/StringBuilder.ap
end:(I)Ljava/lang/StringBuilder;
19: invokevirtual #33 // Method java/lang/StringBuilder.to
tring:()Ljava/lang/String;
22: astore_1
23: getstatic #37 // Field java/lang/System.out:Ljava/
o/PrintStream;
26: aload_1
27: invokevirtual #43 // Method java/io/PrintStream.printl
:(Ljava/lang/String;)V
30: return
Look at number 16 and 19 you can clearly see that it call the StringBuilder class and call append method
internally to the += operator
which appended 42 and in line 19
it then converted it to String.
EDIT:
the above code is actually saying like this: s = s + 42
, so each time you use plus operator to a Integer to the String it will call its wrapper class and call the toString method
JLS
Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression (§15.9):
If T is boolean, then use new Boolean(x).
If T is char, then use new Character(x).
If T is byte, short, or int, then use new Integer(x).
If T is long, then use new Long(x).
If T is float, then use new Float(x).
If T is double, then use new Double(x).
This reference value is then converted to type String by string conversion.
Now only reference values need to 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 an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.