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

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

Moderator: admin

Post Reply
JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post by JeramieH »

Integer i1 = 1;
Integer i2 = new Integer(1);
(i1 == i2) is false

I thought Integers from -128 to 127 were pooled/cached to reuse the same object?

Specifically, I just answered a previous question whose explanation said,
"However, to save on memory, Java 'reuses' all the wrapper objects whose values fall in the following ranges:"

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

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

Post by admin »

Both are correct. Here is the missing information that is causing the confusion:

When you create a primitive wrapper using the new keyword, a new object is created and a cached object, even if available, is not used. For example:
Integer i = 10; //Wrapper created without using the new keyword and is, therefore, cached.
Integer j = 10; //Cached object reused. No new object is created.
Integer k = new Integer(10); //New object is created. Cached object is not reused.

Now it is easy to see why i == j is true but i == k is false.

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

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post by JeramieH »

So basically new always creates a unique distinct object that is independent from the wrapper cache/pool system. Easy enough to remember.

You're the best Paul, just wanted you to know how much we appreciate your work here. I'll owe you a beer when this is all over.

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

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

Post by admin »

You got it :)
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

Actually it is hard to understand this concept, but after i read above posts i think i got an idea about what it is.

When we initialize a primitive wrapper reference with just an integer literal(-128 to 127), it uses reuse technique (pooling).

Integer i = 10;
Integer o = 10;

System.out.println(i==o); > true

But if we initialize with a wrapper object using the 'new' keyword, there is no reuse technique is used

Integer i = new Integer(10);
Integer o = new Integer(10);

System.out.println(i==o); > false


Is that correct????

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

I am reading from a book which says:

"The equals() method in class Object works the same way that the == operator works. If two references point to the same object, the equals() method will return true. If two references point to different objects, even if they have the same values, the method will return false."

So is equals() used in code segment i1.equals(i2) not from class Object? If it is then why the result of the comparison is true ? since i1 and i2 point to two different objects , though with same values?

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

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

Post by admin »

Remember that when you do i1.equals(i2), you are calling equals() on an object of class Integer. Since Integer class overrides Object class's equals method, it is the Integer class's equals method that is invoked. This method, unlike Object class's equals method, checks the value.

Look at it another way - the fact the result is true even though the objects are different, should tell you that Object class's equals is not being used (otherwise, it would have resulted in false). The only way this is possible is if Integer class overrides Object class's equals method :)

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

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Got it Paul. Just like the String.equals() overrides Object.equals(). Thanks for you help, always :)

ramon.carrascom
Posts: 19
Joined: Sun Aug 27, 2017 12:35 pm
Contact:

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

Post by ramon.carrascom »

Just to finish understanding "pooling" questions...

Wrapper class pooling is similar to String pooling, cause when you use "new", it always creates a brand new, non-pooled object and when you initialize using a constant, it is pooled (under the conditions of Wrapper pooling, of using values between -128 and 127). Am I right?

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

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

Post by admin »

That is 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.1369 :

Post by flex567 »

In order this to work they would have to overload the method:

Code: Select all

i1.equals(b1)

Post Reply

Who is online

Users browsing this forum: No registered users and 65 guests