Javatpoint Logo
Javatpoint Logo

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:

DataType Parameter Description Required/Optional
int x It is the first int value to be compared Required
int y It is the second int value to be compared Required

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

Test it Now

Output:

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

Example 2

Test it Now

Output:

x is greater than y

Example 3

Test it Now

Output:

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

Example 4

Test it Now

Output:

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

Next Topicdecode() Method




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA