42 lines
1006 B
Plaintext
42 lines
1006 B
Plaintext
== Why is this an issue?
|
|
|
|
It is preferable to place string literals on the left-hand side of an ``++equals()++`` or ``++equalsIgnoreCase()++`` method call.
|
|
|
|
This prevents null pointer exceptions from being raised, as a string literal can never be null by definition.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
String myString = null;
|
|
|
|
System.out.println("Equal? " + myString.equals("foo")); // Noncompliant; will raise a NPE
|
|
System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Noncompliant; null check could be removed
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
System.out.println("Equal?" + "foo".equals(myString)); // properly deals with the null case
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|