Java.lang.Short.compareTo() Method
Advertisements
Description
The java.lang.Short.compareTo() method compares two Short objects numerically.
Declaration
Following is the declaration for java.lang.Short.compareTo() method
public int compareTo(Short anotherShort)
Parameters
anotherShort -- This is the Short to be compared.
Return Value
This method returns the value 0 if this Short is equal to the argument Short, a value less than 0 if this Short is numerically less than argument Short; and a value greater than 0 if this Short is numerically greater than the argument Short.
Exception
NA
Example
The following example shows the usage of java.lang.Short.compareTo() method.
package com.tutorialspoint;
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);
}
}
Let us compile and run the above program, this will produce the following result:
50 is less than 200, difference = -150 50 is equal to 50, difference = 0 200 is more than 50, difference = 150