java.lang.Short compareUnsigned(short x, short y)
Description
Compares two short values numerically treating the values as unsigned.
Make a note that the compareUnsigned() method of Short class is static thus it should be accessed statically which means the we would be calling this method in this format:
Short.compareUnsigned(long x, long y)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java compareUnsigned method non statically
Method Syntax
public static int compareUnsigned(short x, short y)
Method Argument
Data Type | Parameter | Description |
---|---|---|
short | x | the first short to compare |
short | y | the second short to compare |
Method Returns
The compareUnsigned(short x, short y) method of Short class 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.
Compatibility
Requires Java 1.9 and up
Java Short compareUnsigned(short x, short y) Example
Below is a simple java example on the usage of compareUnsigned(short x, short y) method of Short class.
package com.javatutorialhq.java.examples; /* * This example source code demonstrates the use of * compareUnsigned(short x, short y) method of Short class. */ public class compareUnsignedExample { public static void main(String[] args) { // assign new Short Objects Short w = 2; Short x = 2; Short y = -2; Short z = 5; // w = x int result = Short.compareUnsigned(w,x); System.out.println("Result:"+result); // w less than y (unsigned) result = Short.compareUnsigned(w,y); System.out.println("Result:"+result); // x less than z (numerically) result = Short.compareUnsigned(x,z); System.out.println("Result:"+result); // z greater than w result = Short.compareUnsigned(y,z); System.out.println("Result:"+result); } }
Basically on the above example, we have assigned 4 short primitive w,x,y,and z. There are 4 examples provided, the first one is when we compare w and x, the second one is w and y, third is x and z, while the last is y and z.
In comparing w and x, the result is 0 which is the difference between w and x. Since the result is 0, then we can conclude that w and x is numerically equal.
On the second comparison between w and y, the result is -65532. The result is less than 0 as expected since w is less than y (unsigned). The discussion on signed and unsigned numbers in Mathematics is out of scope but here is the reference if you want to check on it.
Meanwhile the third comparison is between x and z. The result is less than 0. This example is a little bit easier than the second one because both value are +.
For the last example which is the comparison between y and z, the result would be greater than 0. This means that y is greater than z.