In Java, when two objects are compared using ==, their references are also compared. But what happens when they are compared using other relational operators? For instance:
Integer a = new Integer(10);
Integer b = new Integer(9);
if (a >= b) {
System.out.println("A is greater");
}
When I run this, I get output as A is greater, why so? Do references not get compared or they do and it's just a coincidence?
Also, if one of the arguments is primitive, does the non-primitive one get unwrapped to primitive for such comparisons?
In your example, the Integer(s) are unboxed to primitive int(s). There is no <= (or >=) comparison for reference types (without Comparable, Comparator or similar).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With