Page 1 of 1

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

Posted: Wed Feb 26, 2020 5:55 am
by ocpjp11user
Hi)
Here is the question
3-7.png
3-7.png (15.36 KiB) Viewed 2009 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.

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

Posted: Wed Feb 26, 2020 6:32 am
by admin
I am not sure I understand your question. What are you asking?

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

Posted: Wed Feb 26, 2020 6:35 am
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.

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

Posted: Wed Feb 26, 2020 6:56 am
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!