Java - Short compare() Method



The Java Short compare() method compares the two specified short values. The sign of the returned integer value is same as the integer that would be returned by the call −

Short.valueOf(x).compareTo(Short.valueOf(y))

For example, assume we have the first short value s1 as 63 and the second short value s2 as 73. As we can see that the first short value is less than the second short value. Hence, the method will return s1 is less than s2.

is greater than that to the variable y. Then both the values are passed as an argument and compared by if-else statement −
short s1 = 63
short s2 = 73 //Returns s1 is less than s2

Syntax

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

public static int compare(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 x is numerically equal to y.

  • a value less than 0 if x is numerically less than y.

  • a value greater than 0 if x is numerically greater than y.

Comparing two shorts values 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 shows the usage of Java Short compare() method. Two short variables x and y are created. The value assigned to the variable x is greater than that to the variable y. Then both the values are passed as an argument and compared by if-else statement −

package com.tutorialspoint;

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

Output

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

x is greater than y

Comparing two shorts values Example

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

Following is an example of Java Short compare() method. Firstly, two short variables x and y are created. Then the values are assigned to both the variables such that the value of x is less than the value of y. The result is then retrieved after comparing both the values −

package com.tutorialspoint;

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

Output

Following is an output of the above code −

x is less than y

Comparing two same shorts values Example

When the first value passed is equal to the second value, the value returned is 0 using this method.

Following is an example of Java Short compare() method. Two short variables x and y are created and an equivalent value is assigned to both the variables as arguments. Then the values are compared −

package com.tutorialspoint;

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

Output

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

x is equal to y

Comparing two shorts values 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.

In the following example, two short variables value1 and value2 are created with values assigned to it. For comparing the short values, the compare method of the Short class is used.

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.compare(value1, value1));
      System.out.println("value2, value2 : " + Short.compare(value2, value2));
      
      // comparing the first value with the second value
      System.out.println("value1, value2 : " + Short.compare(value1, value2));
      
      // comparing the second value with the first value
      System.out.println("value2, value1 : " + Short.compare(value2, value1));
   }
}

Output

Following is the output of the above code −

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