A RetroSearch Logo

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

Search Query:

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

Short compare() method in Java

Short compare() method in Java

Last Updated : 11 Jul, 2025

The

compare()

method of

Short class

is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short.

Syntax:
public static int compare(short x, short y)
Parameters:

This method accepts two parameters:

Return type :

It returns an

int

value. It returns:

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

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

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

        short a = 4;

        short b = 4;

        // compare method in Short class
        int output = Short.compare(a, 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 compare() method

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

        short a = 4;

        short b = 2;

        // compare method in Short class
        int output = Short.compare(a, 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 compare() method

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

        short a = 2;

        short b = 4;

        // compare method in Short class
        int output = Short.compare(a, 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