I want to compare two Long objects values using if conditions. When these values are less than 128, the if condition works properly, but when they are greater than or equal to 128, comparison fails.
Example:
Long num1 = 127;
Long num2 = 127;
if (num1 == num2) {
// Works ok
}
Comparison on the code above works properly, but fails in the code below:
Long num1 = 128;
Long num2 = 128;
if (num1 == num2) {
// Does NOT work
}
Why is there a problem in comparing Long variables with values greater than 127? If the variables data types are changed to long primitives, then the comparisons work for all cases.