Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
One should always prioritize using a string literal as the first arguments in any string comparison. This approach serves as a preventive measure against NullPointerExceptions because when the second argument is null, instead of encountering an exception, the comparisons will simply yield false results.
Non-Compliant Code Examples
classFoo{booleanbar(Stringx){returnx.equals("42");// should be "42".equals(x)}booleanbar(Stringx){returnx.equalsIgnoreCase("42");// should be "42".equalsIgnoreCase(x)}booleanbar(Stringx){return(x.compareTo("bar")>0);// should be: "bar".compareTo(x) < 0}booleanbar(Stringx){return(x.compareToIgnoreCase("bar")>0);// should be: "bar".compareToIgnoreCase(x) < 0}booleanbaz(Stringx){returnx.contentEquals("baz");// should be "baz".contentEquals(x)}}