1

This is a very basic question I guess but I am not sure how to undestand following So if someone would be so kind

Object a = 128;
Object b = 128;
Log.debug("a: " + ((Integer) a == (Integer)b));
Log.debug("b: " + (((Integer) a).intValue() == ((Integer) b).intValue()));

"a" is false while "b" is true, both are true for a = 127 and b = 127

Lukas Hanacek
  • 1,364
  • 14
  • 25

4 Answers4

7
Object a = 128

is compiled to

Object a = Integer.valueOf(128)

This is called autoboxing.

In some JDK versions, Integer.valueOf() uses a cache of Integer instances usually going from -128 to 127, and returns instances from this cache instead of creating a new Integer instance every time. That's why == returns true for 127 but not for 128.

Don't rely on this, since it's a non-mandatory implementation detail. Always compare their primitive int value or use equals().

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
5

== operator works only for cached values of integer instances. and the cached range for integers is from -128 to 127. In second case however, Integer.intValue() returns an int primitive in which case == operator works as it works in case of primitives.

Related:

Puzzling behaviour of == after postincrementation

Community
  • 1
  • 1
PermGenError
  • 45,977
  • 8
  • 87
  • 106
5

When you're comparing object references, == is defined as being true when the references refer to the same object, not equivalent objects. (That's what equals is for.)

With primitives, == is defined as being true for equivalent values.

In your first example, you have two different objects, one assigned to a and another assigned to b. This is because the primitive you're trying to assign to the reference type is "autoboxed" (automatically wrapped in the equivalent object type for the primitive). Your code:

Object a = 128;
Object b = 128;

...is effectively treated like this:

Object a = Integer.valueOf(128);
Object b = Integer.valueOf(128);

...where Integer.valueOf returns an Integer object wrapping the value you give it, which may or may not be the same object for subsequent calls with the same value, depending on what value you give it. From the Javadoc:

If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

So this means that when you ran it with the value 127, you got the same object back for each call to Integer.valueOf, and so == worked. But for 128, you got different objects back for each call, and so == didn't work, because again it checks that two references refer to the same object, not equivalent ones.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    This doesnt explain why the example works for 127 but not for 128. It becomes clear when you assume that the statements are translated into "Object a = Integer.valueOf(128)" and peek into valueOf()'s javadoc: "Returns an Integer instance representing the specified int value. [...], as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range." – Gyro Gearless Apr 08 '13 at 14:25
  • @GyroGearless: Thank you for that. I both missed that the OP said it worked for 127, and didn't know that about `Integer.valueOf`'s cache. – T.J. Crowder Apr 08 '13 at 14:28
1

Integer.intValue() it converts object to primitive datatype as int

the function of '==' is to check euqality of the item

if we check with the object such as (Integer) a == (Integer)b then it checks the

refrences of that integer values because you typecasted it as object from primitive datatype

and ((Integer) a).intValue() == ((Integer) b).intValue())

it checks actual values not references (Memory Address location where these values are stored

If you want to check real values the use the equals() method to compare their values.

YuvRAJ
  • 94
  • 1
  • 3
  • 10