0

Case 1:

Integer number = 128;
Integer number3 = 128;
System.out.println(number == number3);

Case 2:

Integer number = 127;
Integer number3 = 127;
System.out.println(number == number3);

In this case if the value > 127 it returns false but if the value < 128 then it returns true.

Can somebody explain why case 1 returns false and case 2 returns true?

Bhuwan Gautam
  • 1,229
  • 1
  • 11
  • 24

1 Answers1

1

This happens because Integer class keeps a local cache for values between -128 to 127. You could increase this range as well.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104