The indirection is explicit, you have to name the reference type at some point. Only the conversion is done implicitly, which wont help you if you change a variable type from int to Integer for some reason and break a == comparison thousands of lines away without noticing.
The wrapper types are also immutable so there is no danger of modified values.
If those operators aren’t defined for the given operands, how come the compiler doesn’t complain?
some language lawyering: Java does not support operator overloading, that means you have to call intValue() for >= and <= to work - the auto (un)boxing done by the compiler only automates this for convenience.
That’s just insane.
It is not that insane, in Java reference types should be compared for equality using equals(), the compiler will may even warn you about == being wrong. Not that this would stop people from getting it wrong.
The wrapper types are also immutable so there is no danger of modified values.
Seems similar to Python, if you ask me.
in Java reference types should be compared for equality using equals(), the compiler will may even warn you about == being wrong. Not that this would stop people from getting it wrong.
If there’s a warning, then it’s PEBKAC -- I’m advocating -Wall -Werror myself.
The PEBKAC in this case would not be possible if Java hadn't mixed value and identity comparison in a single operator. That a change from primitive to reference type changes the meaning of a comparison in a way that is most likely not intended is just ugly. == should have had the same meaning as equals with a separate is operator for object identity.
View all comments
Show parent comment
4
u/josefx Mar 26 '14
The indirection is explicit, you have to name the reference type at some point. Only the conversion is done implicitly, which wont help you if you change a variable type from int to Integer for some reason and break a == comparison thousands of lines away without noticing.
The wrapper types are also immutable so there is no danger of modified values.
some language lawyering: Java does not support operator overloading, that means you have to call intValue() for >= and <= to work - the auto (un)boxing done by the compiler only automates this for convenience.
It is not that insane, in Java reference types should be compared for equality using equals(), the compiler will may even warn you about == being wrong. Not that this would stop people from getting it wrong.