- 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
Java Object equals() Method
Description
The Java Object equals(Object obj) method indicates whether some other object is "equal to" this one.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
Declaration
Following is the declaration for java.lang.Object.equals() method
public boolean equals(Object obj)
Parameters
obj − the reference object with which to compare.
Return Value
This method returns true if this object is the same as the obj argument; false otherwise.
Exception
NA
Checking Compatible Objects for Equality Example
The following example shows the usage of java.lang.Object.equals() method. In this example, we've created an Integer Object with value of 50. Now using equals() method, we compared the Integer Object with value of 50 and result is printed.
package com.tutorialspoint; public class ObjectDemo { public static void main(String[] args) { // get an integer, which is an object Integer x = Integer.valueOf(50); // check if x is equal with another int 50 System.out.println("" + x.equals(50)); } }
Output
Let us compile and run the above program, this will produce the following result −
true
Checking Incompatible Objects for Equality Example
The following example shows the usage of java.lang.Object.equals() method. In this example, we've created an Integer object with value of 50 and a Float object with same value. Now using equals() method, we compared both the objects and result is printed.
package com.tutorialspoint; public class ObjectDemo { public static void main(String[] args) { // get an integer, which is an object Integer x = Integer.valueOf(50); // get a float, which is an object as well Float y = Float.valueOf(50f); // check if these are equal,which is // false since they are different class System.out.println("" + x.equals(y)); } }
Output
Let us compile and run the above program, this will produce the following result −
false
Checking Same Objects for Equality Example
The following example shows the usage of java.lang.Object.equals() method. In this example, we've created an Integer Object with value of 50. Now using equals() method, we compared the Integer object with same object and result is printed.
package com.tutorialspoint; public class ObjectDemo { public static void main(String[] args) { // get an integer, which is an object Integer x = Integer.valueOf(50); // check if x is equal with same object System.out.println("" + x.equals(x)); } }
Output
Let us compile and run the above program, this will produce the following result −
true