Java Long - compare() Method
The java.lang.Long.compare() method is used to compare two long values numerically. The value returned is identical to what would be returned by: Long.valueOf(x).compareTo(Long.valueOf(y)).
Syntax
public static int compare(long x, long y)
Parameters
x |
Specify the first long to compare. |
y |
Specify the second long to compare. |
Return Value
Returns the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.
Exception
NA.
Example:
In the example below, the java.lang.Long.compare() method is used to compare given long values.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating long values long val1 = 5; long val2 = 5; long val3 = -5; //comparing long values System.out.println("comparing val1 and val2: " + Long.compare(val1, val2)); System.out.println("comparing val1 and val3: " + Long.compare(val1, val3)); System.out.println("comparing val3 and val1: " + Long.compare(val3, val1)); } }
The output of the above code will be:
comparing val1 and val2: 0 comparing val1 and val3: 1 comparing val3 and val1: -1
❮ Java.lang - Long