Hii, why output is true false in below code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hii, why output is true false in below code ?

Integer a = 127; Integer b = 127; System.out.println(a==b); a=128; b=128; System.out.println(a==b); Please explain

1st May 2020, 2:57 PM
Akshay Harshora
Akshay Harshora - avatar
1 Answer
+ 4
Taking reference from answers in: https://stackoverflow.com/questions/1700081/ Assigning numeric literals to objects of class Integer implicity calls Integer.valueOf(). The .valueOf method was designed in such a way that values in the range of (-128,128) are "pooled". This means that for values of n between -128 and 128, Integer.valueOf(n) returns the same object for each distinct value of n. If you call Integer.valueOf(127) two times, both calls return the same object.
1st May 2020, 3:14 PM
Hatsy Rei
Hatsy Rei - avatar