About Question enthuware.ocajp.i.v7.2.1087 :

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

Moderator: admin

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by raj_dp »

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

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

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by admin »

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.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by raj_dp »

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

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by flex567 »

Regarding last option. Here first Auto - Unboxing occurs and after that Numeric Promotion which promotes short to int.
Is that correct?

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

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by admin »

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

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by flex567 »

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?

Code: Select all

class Test{
    public static void main(String args[]){
		int i = 9;
		Short s = 9;
		System.out.println( s == i );
    }
}
Will integer literal be auto-boxed to Short or do I need to use short literal?

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

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by admin »

This is explained in section 5.1 page 113.
When used on two primitive values or a primitive value and a primitive wrapper, they check whether the two values are same or not.
Several examples are also there. Go through it.

What happened when you compiled and ran your code?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1087 :

Post by flex567 »

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

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests