Java Reference
In-Depth Information
Indicates whether some other object is equal to this one, according to
the following rules.
1. The comparison is reflexive, that is, for any reference value x, x.equals(x)
returns true.
2. The comparison is symmetric, that is, for any reference values x and y,
x.equals(y) returns true if and only if y.equals(x) returns true.
3. The comparison is transitive, that is, for any reference values x, y, and z, if
x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) re-
turns true.
4. The comparison is consistent, that is, for any reference values x and y, mul-
tiple invocations of x.equals(y) consistently return true or consistently re-
turn false, provided no information used in equals comparisons on the
object is modified.
5. For any non-null reference value x, x.equals(null) returns false.
6. The equals() method implements the most discriminating possible equiva-
lence relation on objects; that is, for any reference values x and y, this
method returns true if and only if x and y refer to the same object (x==y has
the value true).
In the implementation of equals() in the classes Double and Float the
method returns true if the two values are the same bit for bit. In other
words, equals() returns true if and only if the numbers are the same, or if
both are both NaN.
Comparisons in IEEE 754
The IEEE 754 Standard specifies that comparisons of floating point num-
bers are exact and never underflow or overflow. The standard defines four
mutually exclusive relations: less than, equal, greater than, and unordered.
The unordered case arises when at least one operand is a NaN. The stan-
dard states that every NaN shall compare unordered with everything, in-
cluding itself and that comparisons shall ignore the sign of zero, so +0 = -0.
Implementations can return one of the four relationships (less than, equal,
greater than, or unordered), or return true or false to predicates that name
the specific comparisons. Java adopts this second option.
The equals() methods of java.lang.Double and java.lang.Float do not
conform to the requirements of IEEE 754 in regards to comparisons.
Comparison of a negative zero to a positive zero returns false. Further-
more, two NaN doubles are considered equal no matter their origin. The
following program demonstrates comparisons using the == operator and
the methods of java.lang.
Search WWH ::




Custom Search