• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

some confusion with Enthuware and ChatGPT

 
Greenhorn
Posts: 5
MySQL Database PHP Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 5823
375
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I would trust Enthuware, a professionally curated and fact checked source, over ChatGPT, a summary of whatever junk it finds on the internet.
 
Enthuware Software Support
Posts: 4898
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replied to the same question a few days ago here: https://enthuware.com/forum/viewtopic.php?f=2&t=6493
 
Paul Anilprem
Enthuware Software Support
Posts: 4898
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 15.18.1 of JLS 21 also says:


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.



So, yes, I think OP's concern is indeed valid. Theoretically, it is an example of autoboxing but, under the hood, autoboxing may not necessarily happen.
 
Alex Uphill
Greenhorn
Posts: 5
MySQL Database PHP Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your answers.

The thread I have linked (in my original post) suggests that I will not get this question on the exam so I am not too worried about it.
 
Marshal
Posts: 80296
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That JLS section sounds like a section I have heard about (probably in JLS ch 17) saying that the JVM is allowed to alter the order of execution of lines (in the original code) provided the semantics of the whole program do not change.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic