FULL PRODUCT VERSION :
sun-jdk-1.8.0_51
ADDITIONAL OS VERSION INFORMATION :
Not relevant
A DESCRIPTION OF THE PROBLEM :
The documentation of Objects.equals states:
"""
Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.
"""
However, it is possible to construct a pathological case which violates this: a class which overrides equals(Object) to return false will return "true" for Objects.equals(a, a), despite a.equals(a) being false.
If a is non-null, then the documentation suggests that you will hit the "Otherwise" case, meaning that a.equals(a) will be invoked, so it should be false. However, it is caught by the "a == a" in the conditional expression, so true is returned.
The documentation should be clarified to reflect this. For example:
"""
if both arguments are null **or refer to the same instance**, true is returned
"""
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
https://ideone.com/f2qBhR
import java.util.Objects;
class Ideone
{
@Override public boolean equals(Object other) {
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone i = new Ideone();
System.out.println(i == null);
System.out.println(i.equals(i));
System.out.println(Objects.equals(i, i));
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
false
false
false
ACTUAL -
false
false
true
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
https://ideone.com/f2qBhR
import java.util.Objects;
class Ideone
{
@Override public boolean equals(Object other) {
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone i = new Ideone();
System.out.println(i == null);
System.out.println(i.equals(i));
System.out.println(Objects.equals(i, i));
}
}
---------- END SOURCE ----------
sun-jdk-1.8.0_51
ADDITIONAL OS VERSION INFORMATION :
Not relevant
A DESCRIPTION OF THE PROBLEM :
The documentation of Objects.equals states:
"""
Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.
"""
However, it is possible to construct a pathological case which violates this: a class which overrides equals(Object) to return false will return "true" for Objects.equals(a, a), despite a.equals(a) being false.
If a is non-null, then the documentation suggests that you will hit the "Otherwise" case, meaning that a.equals(a) will be invoked, so it should be false. However, it is caught by the "a == a" in the conditional expression, so true is returned.
The documentation should be clarified to reflect this. For example:
"""
if both arguments are null **or refer to the same instance**, true is returned
"""
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
https://ideone.com/f2qBhR
import java.util.Objects;
class Ideone
{
@Override public boolean equals(Object other) {
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone i = new Ideone();
System.out.println(i == null);
System.out.println(i.equals(i));
System.out.println(Objects.equals(i, i));
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
false
false
false
ACTUAL -
false
false
true
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
https://ideone.com/f2qBhR
import java.util.Objects;
class Ideone
{
@Override public boolean equals(Object other) {
return false;
}
public static void main (String[] args) throws java.lang.Exception
{
Ideone i = new Ideone();
System.out.println(i == null);
System.out.println(i.equals(i));
System.out.println(Objects.equals(i, i));
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8223112 Clarify operational semantics of java.util.Objects.equals()
- Closed
- relates to
-
JDK-6797535 Add shared two argument static equals method to the platform
- Resolved
-
JDK-8223112 Clarify operational semantics of java.util.Objects.equals()
- Closed