0

What is the explanation for this behavior?

class test {
    public static void main (String[] args) {

    Integer i = 4000, j = 4000;
    System.out.println(i==j);
    Integer k = 50, n =50;
    System.out.println(k==n);

    }

}

Output: False True

pogo
  • 1,479
  • 3
  • 18
  • 23
  • From javadoc `Cache to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive) as required by JLS.` – Thiyagu Aug 08 '18 at 05:20
  • If you are comparing object references, you should get false everywhere. The reason you are getting true in the second case is because both `Integer` instances are pointing to the same object in the integer pool. – Tim Biegeleisen Aug 08 '18 at 05:22

0 Answers0