Java Short compareTo Example
Short class compareTo method example. This example shows you how to use compareTo method.
Short class compareTo method example. This example shows you how to use compareTo method. Method returns less than 0 if this Short is numerically less than the argument Short, if this Short is numerically greater than the argument Short returns greater than 0, returns 0 if this Short is equal to the argument Short.
Here is the code.
/**
* @(#) CompareToShort.java
* A class representing use of method compareTo() of Short class in java.lang Package.
* @Version 03-May-2008
* @author Rose India Team
*/
class CompareToShort{
public static void main(String[] args){
short num1 = 123, num2 = 213, num3 = 123;
Short ShortNum1 = new Short(num1);
Short ShortNum2 = new Short(num2);
Short ShortNum3 = new Short(num3);
//Method returns less than 0 if this Short is numerically less than the argument Short.
int comparisonValue = ShortNum1.compareTo(ShortNum2);
System.out.println("this Short is numerically less than the argument Short.Result is : " + comparisonValue);
//Method returns 0 if this Short is equal to the argument Short.
comparisonValue = ShortNum1.compareTo(ShortNum3);
System.out.println("this Short is numerically equal to the argument Short.Result is : " + comparisonValue);
//Method returns greater than if this Short is numerically greater than the argument Short.
comparisonValue = ShortNum2.compareTo(ShortNum1);
System.out.println("this Short is numerically greater than the argument Short.Result is : " + comparisonValue);
}
} |
|