0

Why is the output of this code true false?

Hey, I recently got this code in a java challenge. Why does it output true false? I can't think of anything that makes sense to me. https://code.sololearn.com/c3wjKZ0hsULV/?ref=app

29th Dec 2018, 11:13 AM
Shoqzz
2 Antworten
+ 8
Integers are cached in range -128 to 127 and every variable has the same memory allocation ///// Java doc source: IntegerCache.low = -128; IntegerCache.high = 127; public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } ///// If value is in range between -128 to 127 method returns cached object (the same memory allocation every time), otherwise create new object. So the best way to compare objects is to use method .equals().
29th Dec 2018, 11:42 AM
Michal
Michal - avatar
+ 3
True if type is int https://code.sololearn.com/c7124lp2wxr2/?ref=app I guess the byte of the object of class Integer is limited : 8 bytes = 1 byte for sign(+/-) + 7 binary digits So cannot store 128 correctly edit: Michal 's equals() method works👍 https://code.sololearn.com/cL76j0ujYduw/?ref=app
29th Dec 2018, 11:40 AM
Gordon
Gordon - avatar