Object Equality Check
About
Object comparison in Java refers to the process of determining whether two objects are considered equal or not based on specific criteria. Comparing two objects in Java can be approached differently depending on the type of objects being compared.
Equality:
Reference Equality (
==
): Compares whether two references point to the same memory address.Value Equality (
equals()
): Compares whether the internal state or content of two objects is the same.
Methods for Comparison:
==
Operator: Tests reference equality, i.e., whether two references point to the same object instance in memory.equals()
Method: Tests value equality, which compares the contents or state of two objects. By default, theequals()
method in Java checks for reference equality (same as==
), but it can be overridden in custom classes to compare based on object contents.
Implementing
equals()
Method:When implementing the
equals()
method in a custom class, it's important to override it to provide a meaningful comparison based on the properties (fields) of the class.The
equals()
method should adhere to the following principles:Reflexive:
x.equals(x)
should always return true.Symmetric: If
x.equals(y)
returns true, theny.equals(x)
should also return true.Transitive: If
x.equals(y)
andy.equals(z)
both return true, thenx.equals(z)
should also return true.Consistent: Repeatedly calling
x.equals(y)
should consistently return true or consistently return false, provided that the objects are not modified.Null Comparison:
x.equals(null)
should return false.
Comparison Strategies:
Primitive Types: Compared using their respective
==
operators for value comparison.Wrapper Classes: Compared using
equals()
for value comparison.String: Compared using
equals()
for content comparison.Arrays: Compared using
Arrays.equals()
orArrays.deepEquals()
for content comparison.Collections (
List
,Set
,Map
): Compared usingequals()
for content comparison.Custom Objects: Implement
equals()
andhashCode()
methods for meaningful comparison based on object properties.
1. Object
Superclass
Object
SuperclassEvery class in Java implicitly inherits from the Object
class, which provides the basic methods for comparison: equals()
and ==
.
==
Operator: Checks for reference equality, i.e., whether two references point to the same object in memory.equals()
Method: Checks for value equality. By default, it behaves like the==
operator (reference equality), but it can be overridden in custom classes to provide meaningful value comparison.
Example
2. Primitive Types
Primitive types in Java are the most basic data types and they directly hold the values. They are not objects and are stored in the stack memory, which makes them fast and efficient.
Comparison: Use the
==
operator to compare primitive values directly. This checks if the values are the same.Performance: Comparisons are fast because they directly compare the values stored in the stack.
Memory: Stored in the stack, which is faster but has limited space.
Primitive Type
Description
Size (bits)
Default Value
Example Comparison Using ==
byte
8-bit integer
8
0
byte a = 1; byte b = 1; System.out.println(a == b); // true
short
16-bit integer
16
0
short a = 1; short b = 1;
System.out.println(a == b); // true
int
32-bit integer
32
0
int a = 1; int b = 1;
System.out.println(a == b); // true
long
64-bit integer
64
0L
long a = 1L; long b = 1L; System.out.println(a == b); // true
float
32-bit floating point
32
0.0f
float a = 1.0f; float b = 1.0f; System.out.println(a == b); // true
double
64-bit floating point
64
0.0d
double a = 1.0; double b = 1.0; System.out.println(a == b); // true
char
16-bit Unicode
16
\u0000
char a = 'a'; char b = 'a'; System.out.println(a == b); // true
boolean
true/false
1
false
boolean a = true; boolean b = true; System.out.println(a == b); // true
Example
3. Primitive Wrapper Classes
Primitive wrapper classes in Java are objects that encapsulate the primitive types. They provide useful methods for manipulating these values and are stored in the heap memory.
Comparison Using
==
: Compares the references, not the values. This checks if the two references point to the same object in memory.Comparison Using
equals()
: Compares the values inside the objects. This checks if the values encapsulated by the objects are the same.Performance: Slightly slower than primitive types because of the overhead of object creation and method calls.
Memory: Stored in the heap, which allows for larger amounts of data but is slower to access than the stack.
Special Cases with Wrapper Classes
Integer Caching: Java caches Integer objects for values between -128 and 127. This means that
Integer
objects within this range will be the same reference if created usingvalueOf()
method or auto-boxing.Boolean and Character Caching:
Boolean
valuestrue
andfalse
are always cached.Character
values from\u0000
to\u007F
are cached.
Wrapper Class
Description
Example Comparison Using ==
(Reference)
Example Comparison Using equals()
(Value)
Byte
Wrapper for byte
Byte a = 1; Byte b = 1; System.out.println(a == b);
// false
System.out.println(a.equals(b)); // true
Short
Wrapper for short
Short a = 1; Short b = 1; System.out.println(a == b);
// false
System.out.println(a.equals(b)); // true
Integer
Wrapper for int
Integer a = 1; Integer b = 1; System.out.println(a == b);
// true (within cache range)
System.out.println(a.equals(b)); // true
Long
Wrapper for long
Long a = 1L; Long b = 1L; System.out.println(a == b);
// true (within cache range)
System.out.println(a.equals(b)); // true
Float
Wrapper for float
Float a = 1.0f; Float b = 1.0f;
System.out.println(a == b);
// false
System.out.println(a.equals(b)); // true
Double
Wrapper for double
Double a = 1.0; Double b = 1.0;
System.out.println(a == b);
// false
System.out.println(a.equals(b)); // true
Character
Wrapper for char
Character a = 'a'; Character b = 'a';
System.out.println(a == b);
// true (within cache range)
System.out.println(a.equals(b)); // true
Boolean
Wrapper for boolean
Boolean a = true; Boolean b = true;
System.out.println(a == b);
// true
System.out.println(a.equals(b)); // true
4. Custom Object Classes
In Java, the equals()
and hashCode()
methods are crucial for custom object classes primarily because they enable proper functionality when instances of these classes are used in collections that rely on hashing, such as HashMap
, HashSet
, and Hashtable
.
equals()
Method
equals()
MethodThe equals()
method in Java is used to compare the equality of two objects based on their internal state or content rather than their memory address (reference equality). By default, the equals()
method in the Object
class compares references (==
operator), which checks if two references point to the same object instance in memory. However, for custom classes, it's often necessary to override equals()
to provide a meaningful comparison based on the attributes or fields of the objects.
Reasons for Implementing equals()
equals()
Semantic Equality: Allows us to define what it means for two instances of our class to be considered equal. This is particularly important when the default reference equality is not sufficient.
Collection Operations: Many Java collections (
HashSet
,HashMap
, etc.) useequals()
to determine if an object is already present in the collection. This is crucial for avoiding duplicates and ensuring proper collection behavior.Consistent Behavior: Provides a clear contract for how equality should be determined across different instances of our class.
Example Implementation
hashCode()
Method
hashCode()
MethodThe hashCode()
method returns a hash code value for an object, which is used by hash-based collections (HashMap
, HashSet
, etc.) to quickly locate objects in memory. Hash codes are essential for efficient storage and retrieval of objects in hash tables.
Reasons for Implementing hashCode()
hashCode()
Efficient Retrieval: Ensures that objects are distributed evenly across the hash table, minimizing collisions and improving performance of hash-based collections.
Contract with
equals()
: Objects that are equal according toequals()
must have the same hash code. This ensures consistency when objects are used in collections.Consistent Behavior: Provides a consistent way to identify objects based on their content, even if they are not the same object instance.
Example Implementation
Custom Object Class Comparison
Overriding equals()
and hashCode()
provides meaningful comparison based on properties.
5. String
In Java, String
is a special class that represents a sequence of characters.
Comparison:
Using
==
: Compares references, not values.Using
equals()
: Compares the actual contents of theString
.
6. Arrays
Arrays in Java are objects that hold a fixed number of values of the same type.
Comparison:
Using
Arrays.equals()
: Compares two arrays for equality based on the values of their elements.
Using
Arrays.deepEquals()
: Compares nested arrays recursively.
7. List, Set, Map
Collections in Java provide a way to group multiple elements into a single unit.
List:
Comparison: Uses equals()
method to compare lists by contents.
Set:
Comparison: Uses equals()
method to compare sets by contents.
Map:
Comparison: Uses equals()
method to compare maps by contents of keys and values.
Primitive Wrapper values
Custom Object values
8. Queue and Stack
Queue:
Comparison: Typically, queues are compared based on their elements using equals()
.
Stack: Comparison: Stacks are compared similarly to lists, based on the equality of their elements.
9. Nested List, Map and Set Comparison
9.1 Nested List
A nested list in Java is a list where the values can themselves be list.
Using equals()
Method:
The
equals()
method ofList
compares the elements of the lists recursively.It ensures that each corresponding pair of nested lists is also compared recursively.
9.2 Nested Map
A nested map in Java is a map where the values can themselves be maps.
Using equals()
Method:
The
equals()
method ofMap
compares the keys and values of the maps recursively.It ensures that each corresponding pair of nested maps is also compared recursively.
9.3 Nested Set
A nested set in Java is a set where each element can itself be a set.
Using equals()
Method:
The
equals()
method ofSet
compares the elements of the sets recursively.It ensures that each corresponding pair of nested sets is also compared recursively.
9.4 List of Map
9.5 List of Set
9.6 Map of String and List
9.7 Set of List
Last updated
Was this helpful?