Wednesday, October 22, 2025
HomeLanguagesJavaShort equals() method in Java with Examples

Short equals() method in Java with Examples

The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method.

Syntax

ShortObject.equals(Object a)

Parameters: It takes an Object type object a as input which is to be compared with the instance of the Short object calling the equals method.

Return Value: It return an boolean value. It returns true if the value of ‘a’ is equal to the value of ShortObject.

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

Example 1:




// Java code to demonstrate
// Short equals() method
  
class GFG {
    public static void main(String[] args)
    {
  
        // creating a Short object
        Short a = new Short("20");
  
        // creating a Short object
        Short b = new Short("20");
  
        // equals method in Short class
        boolean output = a.equals(b);
  
        // Printing the output
        System.out.println("Does " + a
                           + " equals " + b
                           + " : " + output);
    }
}


Output:

Does 20 equals 20 : true

Example 2:




// Java code to demonstrate
// Short equals() 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("20");
  
        // equals method in Short class
        boolean output = a.equals(b);
  
        // Printing the output
        System.out.println("Does " + a
                           + " equals " + b
                           + " : " + output);
    }
}


Output:

Does 2 equals 20 : false
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS