Java Integer compareUnsigned() Method

The compareUnsigned() is a method of Java Integer class which compares two integer objects numerically treating the values as unsigned. It returns the result of the value 0 if both arguments are equal; a value less than 0, if the first argument is less than the second argument; a value greater than 0 will be returned, if the first argument is less than the second argument. This method is specified by Comparable<Integer> Interface.

Note: This method cannot be compared to two different types of argument so, both the argument and the number should be the same type.

Syntax:

Following is the declaration of compareUnsigned() method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
intxIt is the first int value to be comparedRequired
intyIt is the second int value to be comparedRequired

Returns:

The compareUnsigned(int x, int y) method will returns the following values:

  • 0 = The value 0 if x=y,
  • -1 = The value less than 0 if x
  • 1 = The value greater than 0 if x>y

Exceptions:

NA

Compatibility Version:

Java 1.8 and above

Example 1

Output:

Output: 0
Output: -1
Output: 1
Output: -1

Example 2

Output:

x is greater than y

Example 3

Output:

Enter the first value: -45
Enter the second value: 65
First Value is greater than the second value.

Example 4

Output:

Campare the user inputs (unsigned integer): 
-44
-44
88
The Results are: 
0
1
-1