Java - Short compareUnsigned() Method



The Java Short compareUnsigned() method compares the given two short values numerically. This method treats the values as unsigned.

The compareUnsigned() method is static, which means that the class name should be added as a suffix when calling this method. If we will invoke this method non-statically, we will encounter a compilation error. Non-static methods is generally invoked by simply declaring method_name(argument).

Syntax

Following is the syntax for Java Short compareUnsigned() method −

public int compareUnsigned(short x, short y )

Parameters

  • x − This is the first short value to be compared.

  • y − This is the second short value to be compared.

Return Value

This method returns the value as follows −

  • 0 if unsigned value of short x is equal to short y.

  • a value less than 0 if the value of short x is less than short y.

  • a value greater than 0 if the value of short x is greater than short y.

Comparing two short Values in unsigned fashion Example

If the first value passed is greater than the second value to this method, it returns a value greater than 0.

The following example two short variables s1 and s2 are created such that the value of the first variable is greater than the second variable. Then the values are then compared −

package com.tutorialpoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // compares the two specified short values
      short s1 = 86;
      short s2 = 43;
      int retval = Short.compareUnsigned(s1, s2);
      if (retval > 0) {
         System.out.println("s1 is greater than s2");
      } else if (retval < 0) {
         System.out.println("s1 is less than s2");
      } else {
         System.out.println("s1 is equal to s2");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

s1 is greater than s2

Comparing two short Values in unsigned fashion Example

By passing the first value less than the second value to this method, a value less than 0 is returned.

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // compares the two specified short values
      short s1 = 87;
      short s2 = 286;
      int retval = Short.compareUnsigned(s1, s2);
      if (retval > 0) {
         System.out.println("s1 is greater than s2");
      } else if (retval < 0) {
         System.out.println("s1 is less than s2");
      } else {
         System.out.println("s1 is equal to s2");
      }
   }
}  

Output

Following is an output of the above code −

s1 is less than s2

Comparing two short Values in unsigned fashion Example

When the first value passed is equal to the second value, the value returned is 0 by this method. In the following example the values of the variables s1 and s2 are equal when we are call this by passing these two as arguments it returned 0 −

package com.tutorialspoint;

public class ShortDemo {
   public static void main(String[] args) {
      
      // compares the two specified short values
      short s1 = 7;
      short s2 = 7;
      int retval = Short.compareUnsigned(s1, s2);
      if (retval > 0) {
         System.out.println("s1 is greater than s2");
      } else if (retval < 0) {
         System.out.println("s1 is less than s2");
      } else {
         System.out.println("s1 is equal to s2");
      }
   }
}

Output

On executing the program above, the output is obtained as follows −

s1 is equal to s2

Comparing two short Values in unsigned fashion Example

When we pass two integers as an argument using this method, a value less than 0 is returned if the first value is less than the second value; a value greater than 0 is returned if the first value is greater than the second value; and 0 is returned if both the values are equal.

package com.tutorialspoint;

class ShortDemo {
   public static void main(String args[]) {
      short value1 = 84;
      short value2 = 56;
      
      // comparing the same values
      System.out.println("value1, value1 : " + Short.compareUnsigned(value1, value1));
      System.out.println("value2, value2 : " + Short.compareUnsigned(value2, value2));
      
      // comparing the first value with the second value
      System.out.println("value1, value2 : " + Short.compareUnsigned(value1, value2));
      
      // comparing the second value with the first value
      System.out.println("value2, value1 : " + Short.compareUnsigned(value2, value1));
   }
}

Output

While executing the above code, we get the following output −

value1, value1 : 0
value2, value2 : 0
value1, value2 : 28
value2, value1 : -28
java_lang_short.htm
Advertisements