Identity comparison:
Identity
comparison compares the address of values or object references.
Equality comparison:
- Equality comparison compares the values.
- == Operator performs identity comparison for
object types(wrapper classes) in java.
- == Operator performs equality comparison for basic Primitive types
in java.
- equals method performs equality comparison for object
types in java.
int i = 100; int j = 100;
System.out.println(i == j) //will return true as output
Here value 100 is stored in memory at particular location. variable i and j points the same location 100 is present.
Integer i1 = 127; Integer i2 = 127;
Here primitive int 127 is auto boxed to
integer wrapper class and variable i1 is pointing the location in Integer Cache and another 127 is auto boxed to integer
wrapper class and i2 pointing the same location of 127 in Integer Cache.
What is Integer Cache?
Integer cache was introduced in java 5 which was introduced to improve the memory performance for Integer type objects. Integer objects are cached internally and reused via same referenced objects.
1. java.lang.Integer.IntegerCache contains low value -128 and high value 127
There are totally 256 values are cached in Integer cache.
2. The integer caching works only on auto boxing. Integer Objects will not cached when they are built using constructor.System.out.println(i1 == i2) //will return true as output
Here 127 is the last value in
IntegerCache so i1 and i2 pointing the location in Integer cache memory which
is also in same location
System.out.println(i1.equals(i2)) //will return true as output
equals method always unbox the Integer
wrapper class into primitive type and compare the values and uses intValue()
method to unbox to primitive int.
equals method in Integer Class:
1 2 3 4 5 6 | public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; } |
Here equals method convert the wrapper class Integer to primitive int and perform == operation, see the equals method definition in Integer class.
Integer i1=new Integer(127); Integer i2=new Integer(127);
Here primitive int 127 is boxed to integer
wrapper class in 2 new location (i.e) i1 and i2 are pointing to 2 different locations.
System.out.println(i1 == i2) //will return false as output
== always perform identity check
(location check)
Here, though 127 is present as a value in
IntegerCache, because of new Integer() constructor i1 and i2 are created in two
new different location but not in Integer Cache because of different location reference i1 == i2 returns false.
System.out.println( i1.equals(i2)); //will return true as output
equals method always unbox the wrapper
class into primitive type and compare the values and uses intValue() method to
unbox to primitive int
Here equals method unbox the wrapper
class Integer to primitive int and perform == operation, see the equals
definition in Integer class
Integer i1=128; Integer i2=128;
Here 128 is not in Integer Cache, so new location in allocated for 128 refer
valueOf() method in Integer Class.
valueOf method in Integer Class:
1 2 3 4 5 6 | public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); } |
valueOf method checks whether the passing int is in between the low and high cache value if not, then assign new location by calling new Integer constructor.
System.out.println (i1 == i2); //will return false as output
Here the value 128 is not present in
IntegerCache so it gets wrapped like new Integer(128) i1 and i2 are
created in two different location but not in Integer Cache
System.out.println (i1.equals(i2)); //will return true as output
equals always unbox the wrapper class
into primitive type and compare the values and uses intValue() method to unbox
to primitive int.
Here equals method unbox the wrapper
class Integer to primitive int and perform == operation, see the equals
definition in Integer class.
Comparing Integer with int:
Integer i1=128; //wrapper class int i3=128; //primitive int
Here 128 is not in Integer Cache , new
location in allocated for 128.
System.out.println(i1 == i3); //will return true as output
Here i1 is Integer and i3 is primitive
int, so Integer is unboxed to primitive int automatically and perform identity
check of (int 128 == int 128 ).
System.out.println(i1.equals(i3)); //will return true as output.
equals method always unbox the wrapper
class into primitive type and compare the values and uses intValue() method to
unbox to primitive int.
Here equals method unbox the wrapper
class Integer to primitive int and perform == operation, see the equals
definition in Integer class.
Comparison of integer values not
present in Integer cache can be achieved by == operator:
Integer i1 = new Integer(128); //new memory Integer i2 = new Integer(128); //new memory System.out.println (i1 == i2.intValue ()); //will return true as output.
Here equality comparison is achieved programmatically by unboxing the Integer to primitive int.
I hope now you got some idea in this
topic ☺☺☺ Keep Exploring...
Good Explanation
ReplyDeleteIf we summarize simply, please go with equals() method whenever you go for object comparison.
ReplyDelete