6 ms·
The more worrisome thing about equality in Java is that there are integer values for which a == a is true, while a + 1 == a + 1 evaluates to false. (ed
by bunderbunder 9y ago
The more worrisome thing about equality in Java is that there are integer values for which
a == a
is true, while
a + 1 == a + 1
evaluates to false.
(edit: precision)
- deleted 9y ago[deleted]
- jcdavis 9y agoI assume you are referring to the java.lang.Integer box caching situation, which is indeed shitty. However as you've written it, that will always evaluate to true, as it unboxes, adds 1 to each side, and compares the int values, not the references. To get what I assume you are talking about, you need something like: Integer b = a+1; Integer c = a+1; b==c Which by default returns true for a=126 but false for a=127. A bad situation for sure, but thankfully pretty much every static analysis tool will spot that.
- bunderbunder 9y agoYes, you're right. Thanks for correction.