Inspectopedia 2024.3 Help

'compare()' method can be used to compare numbers

Reports expressions that can be replaced by a call to the Integer.compare() method or a similar method from the Long, Short, Byte, Double or Float classes, instead of more verbose or less efficient constructs.

If x and y are boxed integers, then x.compareTo(y) is suggested, if they are primitives Integer.compare(x, y) is suggested.

Example:

public int compare(int x, int y) { return x > y ? 1 : x < y ? -1 : 0; }

After the quick-fix is applied:

public int compare(int x, int y) { return Integer.compare(x, y); }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

UseCompareMethod
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Java language level migration aids

Note that Double.compare and Float.compare slightly change the code semantics. In particular, they make -0.0 and 0.0 distinguishable (Double.compare(-0.0, 0.0) yields -1). Also, they consistently process NaN value. In most of the cases, this semantics change actually improves the code. Use the checkbox to disable this inspection for floating point numbers if semantics change is unacceptable in your case.

New in 2017.2

Inspection options

Here you can find the description of settings available for the 'compare()' method can be used to compare numbers inspection, and the reference of their default values.

Suggest 'Double.compare()' and 'Float.compare()'

Default: Selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.3, Qodana for JVM 2024.3,

Can be installed with plugin:

Java, 243.23126

Last modified: 03 December 2024