+ 5

challange explanation

What is the output of this code? Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2); i1 = 128; i2 = 128; System.out.println(i1 == i2); why the answer is true false

24th Dec 2020, 9:45 PM
ABADA S
ABADA S - avatar
3 ответов
25th Dec 2020, 3:51 PM
Jayakrishna 🇮🇳
+ 1
I remember slightly someone mentioned about value caching for Java Integer. As I recall, value of Integer from -128 up to 127 was cached in memory, and its reference is reused when another Integer was created, and assigned a value within that range (-128 ~ 127). A bit like string pool work for efficiency I guess (cmiiw).
25th Dec 2020, 12:20 AM
Ipang
+ 1
.
16th Mar 2021, 6:11 PM
AbdulGhani Alaya
AbdulGhani Alaya - avatar