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...///
About Question enthuware.ocajp.i.v8.2.1289 :
Moderator: admin
-
- Posts: 14
- Joined: Thu Dec 22, 2016 8:55 pm
- Contact:
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1289 :
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.
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1289 :
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
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1289 :
>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 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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1289 :
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?
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1289 :
Very good
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Google [Bot] and 29 guests