Java.lang Package Classes

Java Integer - compareUnsigned() Method



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

Syntax

public static int compareUnsigned(int x,
                                  int y)

Parameters

x Specify the first int to compare.
y Specify the second int 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.Integer.compareUnsigned() method is used to compare given int values.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating int values
    int val1 = 5;
    int val2 = 5;
    int val3 = 10;

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

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

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