Why do I experience the following behaviour when comparing a Java integer to a Python one of the same value in Jython?
>>> from java.lang import Integer
>>> 10 == Integer(10)
False
>>> 10 <= Integer(10)
True
>>> 10 >= Integer(10)
True
Okay, so I find it weird that both <=
and >=
operators evaluate as expected, yet ==
does not... So now lets check implicit conversions between Java & Jython types:
>>> i = Integer(10)
>>> i == 10
False
What about other Java classes I hear you say? Lets try:
>>> from java.lang import Boolean
>>> Boolean(0) == False
False
>>> Boolean(0) == True
False
>>> Boolean(0) # lets just check it is a Java false not a Python one
false
Am I missing something or is it just as simple as the __eq__
magic (dunder) methods are broken for the Java classes (I am using Jython 2.7)?
EDIT
Thanks to weston for clarifying that in java 10 == new Integer(10)
evaluates as true
and therefore the question is not a duplicate of What is the difference between == vs equals() in Java?
Essentially it's because ==
compares references for boxed types, not the actual values.
But to muddy the waters further <=
and >=
will auto-unbox any boxed operands.