About Question enthuware.ocajp.i.v7.2.1087 :
Moderator: admin
-
- Posts: 16
- Joined: Sun Jun 12, 2016 7:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
Thanks for your explanation.
Yes I can understand that if we can use the "==" operator to compare any primitive numeric variable/ value, it is obvious that it can be used to compare a primitive numeric variable/ value with a primitive Wrapper as the primitive Wrapper shall undergo auto un-boxing and become its corresponding primitive variable and then the two primitive numeric variables can be compared by using "==" operator.
Regards
Raj
Yes I can understand that if we can use the "==" operator to compare any primitive numeric variable/ value, it is obvious that it can be used to compare a primitive numeric variable/ value with a primitive Wrapper as the primitive Wrapper shall undergo auto un-boxing and become its corresponding primitive variable and then the two primitive numeric variables can be compared by using "==" operator.
Regards
Raj
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
But two different numeric wrappers such as Short and Integer cannot be compared using ==. No unboxing will happen here.
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sun Jun 12, 2016 7:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
Yes, if we compare Short and Integer by using '==' operator it will throw compile time error as they are objects and do not have IS-A relation.
Thanks for adding the above point.
Regards
Raj
Thanks for adding the above point.
Regards
Raj
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
Regarding last option. Here first Auto - Unboxing occurs and after that Numeric Promotion which promotes short to int.
Is that correct?
Is that correct?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
Correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
What if one operand should perform 2 actions? Like autoUnbox and Numeric Promotion. Would that work and is there a limit ?
Is there any trick question regarding this?
Will integer literal be auto-boxed to Short or do I need to use short literal?
Is there any trick question regarding this?
Code: Select all
class Test{
public static void main(String args[]){
int i = 9;
Short s = 9;
System.out.println( s == i );
}
}
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
This is explained in section 5.1 page 113.
What happened when you compiled and ran your code?
Several examples are also there. Go through it.When used on two primitive values or a primitive value and a primitive wrapper, they check whether the two values are same or not.
What happened when you compiled and ran your code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1087 :
This code runs fine so I can assume that
- 1 Operand can perform 2 actions
- you don't need a short literal for Short object
- 1 Operand can perform 2 actions
- you don't need a short literal for Short object
Who is online
Users browsing this forum: No registered users and 48 guests