JavaIntermediate#fundamentals

What is the difference between == and .equals() for Integer in Java?

Integer caches values from -128 to 127, so == may return true for small values but false for larger ones. Always use .equals() to compare boxed values.

Example
Integer a = 100, b = 100;
a == b; // true (cached)
Integer c = 200, d = 200;
c == d; // false (not cached)
c.equals(d); // true

Related Questions

1
JavaBeginner#fundamentals

What are Java's primitive data types?

Open
2
JavaIntermediate#fundamentals

What is the difference between pass-by-value and pass-by-reference in Java?

Open
3
JavaBeginner#fundamentals

What is the difference between static and instance methods?

Open