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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

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 2010 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: 8943
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: 8943
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 6 guests