Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

class D {
    public static void main(String args[]) {
        Integer b2=128;
        Integer b3=128;
        System.out.println(b2==b3);
    }
}

Output:

false

class D {
    public static void main(String args[]) {
        Integer b2=127;
        Integer b3=127;
        System.out.println(b2==b3);
    }
}

Output:

true

Note: Numbers between -128 and 127 are true.

8 Answers
8

Leave a Comment