Why Does Integer 200 Behave Differently in Java?🤓
Here’s a small but powerful Java concept that many developers overlook and it can even cause unexpected bugs.
A Java byte has 8 bits:
1 sign bit → 0 = positive, 1 = negative
7 value bits → determine the magnitude.
This gives the range of a byte as:
The smallest value is: 10000000 → -128
The smallest value is: 01111111 → +127
Get prabash withanachchi’s stories in your inbox
Join Medium for free to get updates from this writer.
code 1:
Integer a = 200;
Integer b = 200;
System.out.println(a == b); //false
code 2:
Integer a = 100;
Integer b = 100;
System.out.println(a == b); //true
Why This Happens
Java uses an Integer Cache for efficiency:
Numbers between -128 and 127 are cached and reused
Numbers outside this range (like 200) create new objects
So even if a and b hold the same value, == returns false for numbers outside the cache.





