
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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!