Java - Short equals() Method



The Java Short equals() method is used to compare the equality of the two given values. This method retrieves a Boolean value.

In mathematics, equality is a relationship that states that two quantities have the same value or that two mathematical expressions reflect the same mathematical object. The equality between x and y is denoted as x=y.

Syntax

Following is the syntax for Java Short equals() method −

public boolean equals(Object obj)

Parameters

  • obj − This is the object to compare with.

Return Value

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

Example

If we pass both the objects as an argument similar to each other by this method, it returns true.

The following example shows the comparison of this object against the specified object using Java Short equals() method. Two Short objects ‘obj1’ and ‘obj2’ are created. Then the values are assigned to these objects which are equal to each other. Thereafter, the equality of the values are checked using equals() method −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // compares this object against the specified object
      Short obj1 = new Short("2");
      Short obj2 = new Short("02");
      System.out.print(obj1 + " = " + obj2);
      System.out.println(" ?  " + obj1.equals(obj2));
   }
}

Output

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

2 = 2 ?  true

Example

When we pass both the objects with different values as an argument, false is returned by this method.

In the following example two Short objects ‘obj1’ and ‘obj2’ are created. Then two different values are assigned to these objects to check their equality −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // compares this object against the specified object
      Short obj1 = new Short("87");
      Short obj2 = new Short("45");
      System.out.print(obj1 + " = " + obj2);
      System.out.println(" ?  " + obj1.equals(obj2));
   }
}  

Output

Following is an output of the above code −

87 = 45 ?  false

Example

Following is an example where two Short objects are created and values are assigned to these objects. Then the values are compared using if-else statement. The respective result is then returned −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      
      // same objects
      Short value1 = new Short("2345");
      Short value2 = new Short("2345");
      System.out.print("obj1 & obj2: ");
      if (value1.equals(value2))
         System.out.println("The given two objects are same");
      else
         System.out.println("The given two objects are not same");
      
      // different objects
      value1 = new Short("4567");
      value2 = new Short("789");
      System.out.print("obj1 & obj2: ");
      if (value1.equals(value2))
         System.out.println("The given two objects are same");
      else
         System.out.println("The given two  objects are not same");
   }
}

Output

On executing the program above, the output is obtained as follows −

obj1 & obj2: The given two objects are same
obj1 & obj2: The given two  objects are not same

Example

The following example shows the comparison of this object against the specified object using Java Short equals() method.

import java.util.Scanner;
public class ShortDemo {
   public static void main(String[] args) {
      Short obj = new Short("3");
      System.err.println(
         "Question: What is the full form of SIM ?\n 1.Subscriber's Identity Machine \b 2.Self Identity Machine \b  3.Subscriber's Identity Module");
      Scanner scanner = new Scanner(System.in);
      Short b = 3;
      if (b.equals(obj)) {
         System.out.println("Hurray! You got the correct answer!");
      } else {
         System.out.println("opps! Your answer is incorrect!");
      }
      b = 2;
      if (b.equals(obj)) {
         System.out.println("Hurray! You got the correct answer!");
      } else {
         System.out.println("opps! Your answer is incorrect!");
      }
   }
}  

Output

While executing the above code, we get the following output −

Question: What is the full form of SIM ?
1.Subscriber's Identity Machine 2.Self Identity Machine  3.Subscriber's Identity Module
Hurray! You got the correct answer!
opps! Your answer is incorrect!
java_lang_short.htm
Advertisements