2021-06-08 14:23:48 +02:00
|
|
|
Strings, just like any other ``++Object++``, should be compared using the ``++equals()++`` method.
|
|
|
|
|
|
|
|
Using ``++==++`` or ``++!=++`` compares references rather than values, and usually does not work.
|
|
|
|
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
if (variable == "foo") { /* ... */ }
|
|
|
|
if (variable != "foo") { /* ... */ }
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
if ("foo".equals(variable)) { /* ... */ }
|
|
|
|
if (!"foo".equals(variable)) { /* ... */ }
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
== See
|
|
|
|
|
2021-10-28 10:07:16 +02:00
|
|
|
* https://cwe.mitre.org/data/definitions/597.html[MITRE, CWE-597] - Use of Wrong Operator in String Comparison
|
2021-06-08 14:23:48 +02:00
|
|
|
|
2022-01-25 18:36:46 +01:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::comments-and-links.adoc[]
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|