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'?
2 Antworten
+ 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
0
Thank you HNNX 🐿