Hi, I am confused by a discrepancy between Enthuware and ChatGPT.
According to Enthuware (question enthuware.ocpjp.v17.2.3343)
System.out.println(2+"");
Whenever only one operand of the + operator is a String, the other operand is converted into a String using a string conversion. If the other operand is a numeric primitive type, then it is first converted to a reference type using the boxing conversion and then the boxed reference is used to produce a String. Thus, in this case, 2 will first be boxed into an Integer object and then the Integer object will be used to produce the String "2", which will then be concatenated with "".
Hence, this is a also a valid example where autoboxing occurs.
However ChatGPT says this:
That statement is incorrect (or at least outdated). Java does not autobox an int into an Integer when concatenating to a String. Instead, it uses a string conversion process that does not require boxing.
1. What Actually Happens for 2 + ""
When the Java compiler sees something like:
System.out.println(2 + "");
it essentially transforms the concatenation into something like:
System.out.println(new StringBuilder().append(2).append("").toString());
Specifically:
The string concatenation operation detects that at least one operand ("") is a String.
The other operand (2) is a primitive int.
No autoboxing occurs, because StringBuilder (or StringBuffer) provides an append(int i) overload which accepts an int directly.
Internally, that call effectively does Integer.toString(i) or similar logic, but without creating an Integer object.
Hence, you get "2" appended to "", yielding "2".
2. References in the Java Language Specification
According to the Java Language Specification (JLS), §15.18.1 (String Concatenation Operator +):
If the type of either operand of the + operator is String, then the operation is string concatenation. If the other operand is not a String, it is converted to a string by String.valueOf().
String.valueOf(int i) does not box the integer. It simply returns the decimal representation of i. Internally, it may call Integer.getChars(...) or something similar, but no Integer object is created.
3. Why No Boxing?
Autoboxing is needed when you must convert a primitive to its wrapper object (int -> Integer), typically for using generic types (List<Integer>), passing to a method expecting an Integer, etc.
String concatenation does not require a wrapper object. The compiler calls an append(int) method or String.valueOf(int), both of which handle the primitive int directly.
4. Summary
The older/outdated claim that "the numeric primitive is first boxed into its wrapper type" is not correct in modern Java’s implementation of string concatenation.
In reality, no Integer object is created. The int is converted to a string without autoboxing, typically via String.valueOf(int) or StringBuilder.append(int).
The JLS is not quoted correctly in this answer.
I also found this
Stackoverflow question that suggests GPT is wrong.
Updated I've just seen
this thread and i am even more confused.
What do you guys think?