About Question enthuware.ocpjp.v21.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
n199a_
Posts: 8
Joined: Wed Jan 08, 2025 9:43 am
Contact:

About Question enthuware.ocpjp.v21.2.3343 :

Post by n199a_ »

Identify examples of autoboxing.
[+] Integer i = 10;
[+] Integer getValue(){ return 2; }
[-] Long getValue(){ return 2; } - An int cannot be autoboxed into a Long. return 2L; would have been valid.
[+] System.out.println(2+"");

For some reason, the correct option is

Code: Select all

System.out.println(2+"");
However, we have excellent Java documentation (https://docs.oracle.com/javase/tutorial ... oxing.html), which says it all, quote:
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
Therefore, your item "System.out.println(2+"");" should be marked as INCORRECT.

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

Re: About Question enthuware.ocpjp.v21.2.3343 :

Post by admin »

Did you read the explanation? The expression 2+"" will cause primitive value 2 to be autoboxed into Integer object containing 2 first.

As per JLS 21 section 15.18:
If the type of either operand of a + operator is String, then the operation is string concatenation.
...
If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.
and section 5.1.11 (String conversion):
If T is byte, short, or int, then use new Integer(x).
So it is quite clear that autoboxing will be used while evaluating 2+"".

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests