0

Not able to understand the output. requesting to explain

public class MyClass { public static void main(String args[]) { Integer x = 127; Integer y = 127; System.out.println (x==y); x=128; y=128; System.out.println(x==y); } } Output True False Please explain why 2nd output is false

5th Dec 2020, 9:24 PM
Milind Ghag
Milind Ghag - avatar
4 odpowiedzi
+ 5
It is because the Integer class maintains an internal cache which doesn't require the instantiation of an object to store values which are frequently used from range -128 to 127 which is 1 byte. For all the values greater than that it creates separate objects. And hence the == will return false. https://code.sololearn.com/cn2vMn0Uyr74/?ref=app
5th Dec 2020, 9:46 PM
Avinesh
Avinesh - avatar
+ 1
type of int is primitive while Integer is of class type
5th Dec 2020, 9:45 PM
Armin Akhavan
Armin Akhavan - avatar
+ 1
Thank you so much for response. Really helped
6th Dec 2020, 2:37 PM
Milind Ghag
Milind Ghag - avatar
0
Use int x & int y Happy coding ;))
5th Dec 2020, 9:43 PM
Armin Akhavan
Armin Akhavan - avatar