About Question enthuware.ocajp.i.v8.2.1289 :

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

Moderator: admin

Post Reply
sulakshana
Posts: 14
Joined: Thu Dec 22, 2016 8:55 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1289 :

Post by sulakshana »

i cant understand this question please explain??????
how it is going to add INTEGER and int....
where it autoboxes and where it unboxes etc....
I need to know how last statement works...///

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

Re: About Question enthuware.ocajp.i.v8.2.1289 :

Post by admin »

I am not sure what you do mean by "how it is going to add Integer and int". The JVM will unbox both the Integer operands, add them and then assign the result to the int or Integer variable.

In the last statement, since you are assigning the result back to an Integer variable, the result be boxed by the JVM into an Integer object.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1289 :

Post by OCAJO1 »

On the same lines as this question,

Short ss = 2;
Integer is = 5;
Long ls = ss + is; //will cause an obvious compiler error.
Long ls = (long) ss + is; //this does not.

I think I've forgotten how casting of one the variable effects the other.

Thanks

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

Re: About Question enthuware.ocajp.i.v8.2.1289 :

Post by admin »

>Long ls = ss + is; //will cause an obvious compiler error.
If you know the reason for this then you should know the reason for the next one also. It compiles for the same reason!
>Long ls = (long) ss + is; //this does not.

Go through the rules for primitive widening conversion. Also check precedence of cast operator.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1289 :

Post by OCAJO1 »

Ok after a refresher, lets see if my reasoning for this makes any sense,

For the one that does not compile,

1. both ss and is are unboxed.
2. ss will be widened to int
3. the addition result will be an int
4. the result will be boxed into Integer
5. Integer can not be assigned to Long

For the one that does compile,

1. ss is cast as long so it will be unboxed into short and then widened into long.
2. is will be unboxed to int
3. addition result will be a long
4. the result will be boxed into a Long in order to be assigned to Is

Did I miss anything?

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

Re: About Question enthuware.ocajp.i.v8.2.1289 :

Post by admin »

Very good :thumbup:
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests