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

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

Moderator: admin

Post Reply
vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post by vchhang »

Would explain the scenario where the wrapper class interned the values between -128 and 127? My apologies for being vague but I do not understand it enough to be more clear.

Apparently it does not applies in this case "new Integer(1);". I thought it would be interned for both Integer and Long since it is below the range described earlier.

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

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

Post by admin »

Well, you can use the same code and replace the lines
Integer i = new Integer(1) ;
Long m = new Long(1);
with
Integer i = 1 ; //instead of 1, you may try with other values as well.
Integer m = 1;

Interning does not happen when you do a new of wrapper classes.

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

sgomez
Posts: 1
Joined: Sun Jul 24, 2016 6:04 am
Contact:

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

Post by sgomez »

As the equals() method could be overriden, we cannot predict the behaviour in the IF statement.
In this case, Why the answer can not be "None of the above"??

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

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

Post by admin »

All wrapper classes are final. So you can't override equals method.
If you like our products and services, please help us by posting your review here.

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson »

Here is the official declaration for "equals" method in Integer class:

"Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object."

but it our example we have Integer object and Long object. So why compile time error is not thrown?

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson »

Or is it ok to pass any Object, but the method will return true if and only if passed value is an Integer.class?

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

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

Post by admin »

Yes, since equals method is defined in Object class and that equals method accepts Object as an argument, any class that overrides the equals method must accept any object as an argument. There will be NO compilation error for this reason. However, at run time, the overridden equals method may check the type of the object passed and its value and then return the result true or false accordingly.
If you like our products and services, please help us by posting your review here.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

Post by kevin35 »

Hi Paul,

can you explain why this is allowed
short s = 10;
Short S = 10; //why dont need cast?

compile error:
long l = 10;
Long L = 10; //has to be 10L or (long)10


By assigning a primitive to a wrapper class variable (autoboxing) the compiler converts it to:
Short S=Short.valueOf((short)10); //here you need explicit cast

when do you need to explicit cast it and when not?

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

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

Post by admin »

Compiler can and does consider compile time constant values to determine whether the assignment is valid or not. 10 is an int but it is also a compile time constant that fits into a short, so the compiler performs a narrowing primitive conversion from int to short without requiring a cast. That is why short s = 10; compiles fine.

Short S = 10; compiles fine because section 5.2 allows a narrowing primitive conversion followed by a boxing conversion if the value of the constant expression is representable in the type short and the target variable is of type Short.

long l = 10; compiles fine because int is a smaller primitive type than long. There is no complication here.

The reason why Long L = 10; does not compile is tricky. It does not compile because it is not one of the allowed conversions as given by JLS section 5.2. Assigning 10 to Long will require a primitive widening and then a boxing conversion. But the specification does not allow widening+boxing (unlike in the case of Short, where it allows narrowing+boxing).

All this is explained in detail here: https://docs.oracle.com/javase/specs/jl ... jls-5.html
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post by zel_bl »

I have a similar doubt as in the previous post, only concerning constructor in this question.... Long(long value) ....
Long l = new Long(1); works fine. But is it not that I have widening first (from int to long) and then boxing (long to Long), which is not allowed. Yet it works in this case, unless I understand something wrongly.
I'd appreciate if you could clarify it.

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

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

Post by admin »

When you do new Long(1);, 1 is indeed widened to long. But why do you think it is further boxed into Long?? The Long(long ) constructor is used.
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post by zel_bl »

Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests