mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
27 lines
975 B
HTML
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> |