Java Short - compareTo() Method
The java.lang.Short.compareTo() method is used to compare two Short objects numerically.
Syntax
public int compareTo(Short anotherShort)
Parameters
anotherShort |
Specify the Short to be compared. |
Return Value
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 the argument Short; and a value greater than 0 if this Short is numerically greater than the argument Short (signed comparison).
Exception
NA.
Example:
In the example below, the java.lang.Short.compareTo() method is used to compare given Short objects.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating Short objects Short val1 = 5; Short val2 = 5; Short val3 = -5; //comparing Short objects System.out.println("comparing val1 with val2: " + val1.compareTo(val2)); System.out.println("comparing val1 with val3: " + val1.compareTo(val3)); System.out.println("comparing val3 with val1: " + val3.compareTo(val1)); } }
The output of the above code will be:
comparing val1 with val2: 0 comparing val1 with val3: 10 comparing val3 with val1: -10
❮ Java.lang - Short