Files
openide/java/java-impl/resources/inspectionDescriptions/ComparatorResultComparison.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

27 lines
975 B
HTML

<html>
<body>
Reports comparisons of the result of <code>Comparator.compare()</code> or <code>Comparable.compareTo()</code> calls with non-zero constants.
By contract, these methods can return any integer (not just -1, 0 or 1),
so comparing against particular numbers is bad practice. Some widely used comparison methods (e.g. <code>String.compareTo()</code>)
actually return values outside the [-1..1] range, and such a comparison may cause incorrect program behavior.
<p>Example:</p>
<pre><code>
void validate(String s1, String s2) {
// Comparing to 1 is incorrect
if (s1.compareTo(s2) == 1) {
throw new IllegalArgumentException("Incorrect order");
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
void validate(String s1, String s2) {
if (s1.compareTo(s2) > 0) {
throw new IllegalArgumentException("Incorrect order");
}
}
</code></pre>
<!-- tooltip end -->
<p><small>New in 2017.2</small></p>
</body>
</html>