Java.lang Package Classes

Java Long - compareUnsigned() Method



The java.lang.Long.compareUnsigned() method is used to compare two long values numerically treating the values as unsigned.

Syntax

public static int compareUnsigned(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 as unsigned values; and a value greater than 0 if x > y as unsigned values.

Exception

NA.

Example:

In the example below, the java.lang.Long.compareUnsigned() 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 = 10;

    //comparing long values 
    System.out.print("comparing val1 and val2: "); 
    System.out.println(Long.compareUnsigned(val1, val2)); 

    System.out.print("comparing val1 and val3: ");
    System.out.println(Long.compareUnsigned(val1, val3)); 

    System.out.print("comparing val3 and val1: "); 
    System.out.println(Long.compareUnsigned(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