2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
It is equivalent to use the equality ``++==++`` operator and the ``++equals++`` method to compare two objects if the ``++equals++`` method inherited from ``++Object++`` has not been overridden. In this case both checks compare the object references.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
But as soon as ``++equals++`` is overridden, two objects not having the same reference but having the same value can be equal. This rule spots suspicious uses of ``++==++`` and ``++!=++`` operators on objects whose ``++equals++`` methods are overridden.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:47:33 +02:00
----
String firstName = getFirstName(); // String overrides equals
String lastName = getLastName();
if (firstName == lastName) { ... }; // Non-compliant; false even if the strings have the same value
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2020-06-30 12:47:33 +02:00
----
String firstName = getFirstName();
String lastName = getLastName();
if (firstName != null && firstName.equals(lastName)) { ... };
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
2021-01-27 13:42:22 +01:00
Comparing two instances of the ``++Class++`` object will not raise an issue:
2020-06-30 14:49:38 +02:00
2023-05-25 14:18:12 +02:00
[source,java]
2020-06-30 12:47:33 +02:00
----
Class c;
if(c == Integer.class) { // No issue raised
}
----
2021-01-27 13:42:22 +01:00
Comparing ``++Enum++`` will not raise an issue:
2020-06-30 14:49:38 +02:00
2023-05-25 14:18:12 +02:00
[source,java]
2020-06-30 12:47:33 +02:00
----
public enum Fruit {
APPLE, BANANA, GRAPE
}
public boolean isFruitGrape(Fruit candidateFruit) {
return candidateFruit == Fruit.GRAPE; // it's recommended to activate S4551 to enforce comparison of Enums using ==
}
----
2021-01-27 13:42:22 +01:00
Comparing with ``++final++`` reference will not raise an issue:
2020-06-30 14:49:38 +02:00
2023-05-25 14:18:12 +02:00
[source,java]
2020-06-30 12:47:33 +02:00
----
private static final Type DEFAULT = new Type();
void foo(Type other) {
if (other == DEFAULT) { // Compliant
//...
}
}
----
2021-01-27 13:42:22 +01:00
Comparing with ``++this++`` will not raise an issue:
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2023-05-25 14:18:12 +02:00
[source,java]
2020-06-30 12:47:33 +02:00
----
public boolean equals(Object other) {
if (this == other) { // Compliant
return false;
}
}
----
2021-01-27 13:42:22 +01:00
Comparing with ``++java.lang.String++`` and boxed types ``++java.lang.Integer++``, ... will not raise an issue.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:47:33 +02:00
* S4973 - Strings and Boxed types should be compared using "equals()"
2024-01-15 17:15:56 +01:00
* CWE - https://cwe.mitre.org/data/definitions/595[CWE-595 - Comparison of Object References Instead of Object Contents]
* CWE - https://cwe.mitre.org/data/definitions/597[CWE-597 - Use of Wrong Operator in String Comparison]
2020-12-21 15:38:52 +01:00
* https://wiki.sei.cmu.edu/confluence/x/UjdGBQ[CERT, EXP03-J.] - Do not use the equality operators when comparing values of boxed primitives
* https://wiki.sei.cmu.edu/confluence/x/yDdGBQ[CERT, EXP50-J.] - Do not confuse abstract object equality with reference equality
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]