0

Why the output is true and false?

Question is: Integer i1=127; Integer i2=127; System.out.print(i1==i2); i1=128; i2=128; System.out.print(i1==i2); Can anyone tell me why output is 'true false'?

31st May 2020, 2:57 PM
Nikhil Maroju
Nikhil Maroju - avatar
2 Respuestas
+ 4
Sure Java stores integers till 127 as primitive and you can compare primitives with == operator However Integer x = 128 is not primitive - it's a wrapper class and should be compared using proper methods i1.CompareTo(i2); Read more about it here (object interning) https://stackoverflow.com/questions/10149959/using-operator-in-java-to-compare-wrapper-objects
31st May 2020, 3:51 PM
HNNX 🐿
HNNX 🐿 - avatar
0
Thank you HNNX 🐿
1st Jun 2020, 3:40 AM
Nikhil Maroju
Nikhil Maroju - avatar