Under the Hood / Part 8YOUTUBE PLAYER LOADS ON PLAYWhy 127 == 127 but 128 != 128
Why
Why 127 == 127 but 128 != 128 in Java (Integer Cache) | Under the Hood
In this lesson
In Java, 127 == 127 is true, but 128 == 128 is false. Same code, different answer. Here's why.
→ Integer is an object, not a primitive. Assigning a number boxes it → Behind the scenes, the compiler calls Integer.valueOf() → valueOf caches Integer objects from -128 to 127 → 127 twice → the same cached object → == is true → 128 → a new object each time → == is false → The fix: equals(), or compare plain ints. Long, Short and Byte have the same cache
Under the Hood: how Java really works.
Instagram: @thecodestormdev
Under the Hood, 8 out
1Java HashMap Explained Visually: hashCode, Buckets, put and get | Part 11:302Java HashMap Collisions, Treeify and Resize Explained Visually | Part 21:393The equals and hashCode Contract in Java HashMap, Explained Visually | Part 31:354JDK vs JRE vs JVM: How Java Really Runs Your Code1:335Stack vs Heap in Java: Where Your Variables Actually Live1:436Java Is Always Pass-by-Value (Even for Objects)1:297Why == Fails on Strings in Java (The String Pool)1:158Why 127 == 127 but 128 != 128 in Java (Integer Cache)1:21Keep going
More Shorts.
Why == Fails on Strings in Java (The String Pool) | Under the Hood, Short.
Java Is Always Pass-by-Value (Even for Objects) | Under the Hood, Short.
Stack vs Heap in Java: Where Your Variables Actually Live | Under the Hood, Short.
JDK vs JRE vs JVM: How Java Really Runs Your Code | Under the Hood, Short.
