Java - Short compareTo() Method



The Java Short compareTo() method compares two Short objects numerically.

This method is used when we need to compare the given two values of Short objects to check whether these two given values are equal, less than or greater than.

In an object, the Short class wraps the short type. The short primitive data type is a 16-bit signed two’s complement integer that has a maximum value of 32,767 and a minimum value of -32,768 (inclusive).

Syntax

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

public int compareTo(Short anotherShort) 

Parameters

  • anotherShort: This is the short value to be compared.

Return Value

This method returns the value as follows −

  • 0 if anotherShort is numerically equal to this short.

  • a value less than 0 if this short is numerically less than anotherShort.

  • a value greater than 0 if this short is numerically greater than anotherShort.

Example

When we pass two short values to this method, a value less than 0 is returned if the first value is less than the second value.

The following example shows the usage of Java Short compareTo() method. In here, two Short objects ‘obj1’ and obj2’ are created. The values are assigned to the given two objects such that the first value is less than the second value. The result is then retrieved after comparing both the values −

import java.lang.*; 
public class ShortDemo {
   public static void main(String[] args) {
      
      // compares two Short objects numerically
      Short obj1 = new Short("987");
      Short obj2 = new Short("7676");
      int retval = obj1.compareTo(obj2);
      if (retval > 0) {
         System.out.println("obj1 is greater than obj2");
      } else if (retval < 0) {
         System.out.println("obj1 is less than obj2");
      } else {
         System.out.println("obj1 is equal to obj2");
      }
   }
}

Output

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

obj1 is less than obj2

Example

When we pass multiple integers as an argument to 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 three Short objects are created with the name Shortval1, Shortval2 and Shortval3. Then these objects are assigned with the value of '50', '200', '50' short variables. The compareTo() method is then called on the object for comparing all the objects in different given cases −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // create short object and assign value to it
      short val1 = 50, val2 = 200, val3 = 50;
      Short Shortval1 = new Short(val1);
      Short Shortval2 = new Short(val2);
      Short Shortval3 = new Short(val3);
      
      // returns less than 0 if this Short is less than the argument Short
      int cmp = Shortval1.compareTo(Shortval2); 
      System.out.println("" + Shortval1 + " is less than " + Shortval2 + ",difference = " + cmp);
      
      // returns 0 if this Short is equal to the argument Short
      cmp = Shortval1.compareTo(Shortval3); 
      System.out.println("" + Shortval1 + " is equal to " + Shortval3 + ",difference = " + cmp);
      
      // returns greater than if this Short is greater than the argument Short
      cmp = Shortval2.compareTo(Shortval1); 
      System.out.println("" + Shortval2 + " is more than " + Shortval1 + ",difference = " + cmp);
   }
}   

Output

Following is an output of the above code −

50 is less than 200, difference = -150
50 is equal to 50, difference = 0
200 is more than 50, difference = 150

Example

When we create an array of Short object using this method, the values greater than 0, less than 0 and equal to 0 are returned respectively comparing with the first element in the array.

In this example, an array of short type is created, and each object in the array is compared to the array's first object by assigning a for loop −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short[] object = { -854, -546, -854, -917 };
      for (int i = 0; i < 4; i++) {
         
         // comparing every object in the array with the first object            
	      System.out.println(object[0].compareTo(object[i]));
      }
   }
}

Output

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

0
-308
0
63
java_lang_short.htm
Advertisements