About Question com.enthuware.ets.scjp.v6.2.55 :

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

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.55 :

Post by TwistedLizard »

Regarding option 6
Short k = 9; Integer i = 9; System.out.println(k == i);
I understand the given reason why it won't compile, but the statement

Code: Select all

Short k =9;
mystifies me somewhat.
I know it actually does work, but the literal 9 is an int literal. So, why doesn't it box to an Integer which would be unassignable to a Short variable?

In the case of primitive assignments such as

Code: Select all

byte b = 9;
again 9 is an int literal, but I can make sense of the statement by imagining that the compiler inserts an implicit cast

Code: Select all

byte b = (byte)9;
Is the best way to think of it, that in this situation also, the compiler inserts a cast to short which is then boxed?

Code: Select all

Short k = (short)9;
[I manually created the subject as clicking on Discuss in the viewer brings up the wrong page.]

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

Re: About Question com.enthuware.ets.scjp.v6.2.55 :

Post by admin »

Because section 5.2 of JLS clearly says:
A narrowing primitive conversion followed by a boxing conversion may be used
if the type of the variable is:
– Byte and the value of the constant expression is representable in the type byte


Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests