Infosys Interview Question
AnalystsCountry: India
Interview Type: Written Test
(1)Comparing the two refs using '!=' is comparing the two objects' addresses.
(2)Comparing the two refs using '>=' or '<=' is comparing the two integer values(auto unboxing).
But if >= returns true how the other <= can return false if it is comparing the two integer values?
The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.
In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.
For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.
Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions
The output is :
true
true
true
The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.
In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.
For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.
Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions
The simple answer is:
- Integer is an Object so when you use == or != it will compare reference. As they are two different instance, the first comparison is true.
- When you use >= or <= operators, the Integer is "unboxed" (meaning that it's value is compared instead of the reference) so both second and third cases would return true.
Wrapper classes behaves same as String class in java when it comes to .equals(),== or != operations.
The wrapper classes(Integer in this case) will behave exactly the same as the String class would have behaved.
Please imagine the String in-place of Integer, and the answer will become crystal clear.
For <= or >= operator, the values will be compared as these operations cannot be done on objects (Common sense :))
The output is :
- dhirajb1989 May 10, 2014true
true
true
The concept here is the same as String constant pool
Java maintains an integer constant pool for integers upto -128 to 127.
So if you do
{{ Integer i1 =127;
Integer i2 = 127;
System.out.println(i1 == i2);
}}
Then it will give true as it is returning the same object from the constant pool.
In the above Question
{{
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
}}
Both are different object not created in the pool but somewhere else in the heap. So they have different addresses and so hashcode generated for them is different due to which the answer will be false.
For {{System.out.println(i1 <= i2);}}, actual unboxing is happening due to which addresses are not being compared but 1<=1 is being compared.
Instead of ==, we should use equals() method as it properly matches the contents of the two objects and then makes it decisions