Page 1 of 1

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

Posted: Wed May 07, 2014 11:02 am
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:"

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

Posted: Wed May 07, 2014 11:25 am
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.

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

Posted: Wed May 07, 2014 3:14 pm
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.

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

Posted: Wed May 07, 2014 7:54 pm
by admin
You got it :)

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

Posted: Thu Jul 03, 2014 11:33 am
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????

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

Posted: Thu Jul 03, 2014 10:47 pm
by admin
Yes, that is correct.

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

Posted: Mon Apr 10, 2017 1:04 am
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?

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

Posted: Mon Apr 10, 2017 1:16 am
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.

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

Posted: Mon Apr 10, 2017 1:40 am
by mjmsausava
Got it Paul. Just like the String.equals() overrides Object.equals(). Thanks for you help, always :)

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

Posted: Mon Sep 25, 2017 1:31 pm
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?

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

Posted: Mon Sep 25, 2017 7:41 pm
by admin
That is correct.

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

Posted: Thu May 09, 2019 10:06 am
by flex567
In order this to work they would have to overload the method:

Code: Select all

i1.equals(b1)