Java.lang.Short.equals() Method Example



The java.lang.Short.equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is a Short object that contains the same short value as this object.


Following is the declaration for java.lang.Short.equals() method

public boolean equals(Object obj)


obj − This is the object to compare with.

Return Value

This method returns true if the objects are same, else false.




The following example shows the usage of java.lang.Short.equals() method.

package com.tutorialspoint;

import java.lang.*;

public class ShortDemo {

   public static void main(String[] args) {

      // create short object and assign value to it
      short shortNum1 = 200, shortNum2 = 200, shortNum3 = 50;
      Short ShortObj1 = new Short(shortNum1);
      Short ShortObj2 = new Short(shortNum2);
      Short ShortObj3 = new Short(shortNum3);

      // returs false as both object contains different short value
      boolean val = ShortObj1.equals(ShortObj3); 
      System.out.println("shortObj1 and shortObj3 have same value : " + val);

      // returns true as both object contains same short value
      val = ShortObj1.equals(ShortObj2); 
      System.out.println("shortObj1 and shortObj2 have same value : " + val);

Let us compile and run the above program, this will produce the following result −

shortObj1 and shortObj3 have same value : false
shortObj1 and shortObj2 have same value : true