A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/short-compareto-method-in-java-with-examples/ below:

Short compareTo() method in Java with Examples

Short compareTo() method in Java with Examples

Last Updated : 11 Jul, 2025

The

compareTo()

method of

Short class

is a built in method in Java which is used to compare twoShort objects numerically.

Syntax:
public int compareTo(Short otherShort)
Parameters

: This method accepts a mandatory parameter

otherShort

which is the Short object to be compared.

Return type :

It returns an

int

value. It returns:

Below is the implementation of compareTo() method in Java:

Example 1: Java
// Java code to demonstrate
// Short compareTo() method

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

        // creating a Short object
        Short a = new Short("4");

        // creating a Short object
        Short b = new Short("4");

        // compareTo method in Short class
        int output = a.compareTo(b);

        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
Output:
Comparing 4 and 4 : 0
Example 2: Java
// Java code to demonstrate
// Short compareTo() method

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

        // creating a Short object
        Short a = new Short("4");

        // creating a Short object
        Short b = new Short("2");

        // compareTo method in Short class
        int output = a.compareTo(b);

        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
Output:
Comparing 4 and 2 : 2
Example 3: Java
// Java code to demonstrate
// Short compareTo() method

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

        // creating a Short object
        Short a = new Short("2");

        // creating a Short object
        Short b = new Short("4");

        // compareTo method in Short class
        int output = a.compareTo(b);

        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}
Output:
Comparing 2 and 4 : -2


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4