About Question enthuware.ocpjp.ii.v11.2.3343 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ocpjp11user
Posts: 6
Joined: Mon Jan 13, 2020 10:05 pm
Contact:

About Question enthuware.ocpjp.ii.v11.2.3343 :

Post by ocpjp11user »

Hi)
Here is the question
3-7.png
3-7.png (15.36 KiB) Viewed 13026 times
The question is regarding firth option

Code: Select all

System.out.println(2+"");
When you concatenate anything with a String, `toString()` is called on that object and then concatenation happens. Since 2 is primitive, it first Autoboxed to Integer, and then

Code: Select all

Integer.toString
method is called.

p.s. If it was just

Code: Select all

System.out.println(2);
- that's fine no autoboxing, cause println - overloaded method and can take primitive int.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Possible error in ocp-816(3-7)

Post by admin »

I am not sure I understand your question. What are you asking?
If you like our products and services, please help us by posting your review here.

ocpjp11user
Posts: 6
Joined: Mon Jan 13, 2020 10:05 pm
Contact:

Re: Possible error in ocp-816(3-7)

Post by ocpjp11user »

The code `2+""`, will cause autoboxing, cause in order to concatenate primitive with string, java first transform primitive into object, and then call `toString()` on that object. So, option 5 is also correct answer.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Possible error in ocp-816(3-7)

Post by admin »

Oh ok, I see what you mean. Yes, that is a good point. Autoboxing does happen in this case with the non string operand. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests