Short compareTo() in Java

Short compareTo(): This method is available in java.lang.Short class of Java.

Syntax:

int java.lang.Short.compareTo(Short anotherShort)

This method takes one argument of type Short as its parameter. This method compares two Short objects numerically.

Parameters: One parameter is required for this method.

anotherShort: the Short to be compared.

Returns:

1. The value 0 if this Short is equal to the argument Short.

2. A value less than 0 if this Short is numerically less than the argument Short.

3. A value greater than 0 if this Short is numerically greater than the argument Short (signed comparison).

Exceptions: NA

Approach

Java

public class ShortcompareTo {
    public static void main(String[] args) {

        Short short1 = 1;
        Short anotherShort = 2;
        System.out.println(short1.compareTo(anotherShort));
    }
}

Output:

-1

No comments:

Post a Comment