sneppets-java8

Compare two objects using equals() method of Object Class

This tutorial guides you on how to compare two objects using equals() method of Object class in Java. There is a class called “Object” in “java.lang” package which is the super class of all classes in java. Method equals() is one of the methods of Object class which will be used to compare objects.

Compare two objects using equals() method

Method equals() of Object class is used to compare the references of two objects and if they are equal, this method returns true or else it will return false. It means this method returns true, if the both the references refer to the same object, otherwise it returns false.

But when you compare String objects or Wrapper class objects like Integer, Float, Double etc., it actually compares the contents of the objects. In this case equals() method returns true only if the contents of the objects are same, otherwise it returns false.

Let’s discuss in details with examples for these cases: compare object references, String objects and wrapper class objects.

Compare two object’s references using equals()

The following program shows how to compare object references using equals() method of Object class. In this example, let’s create two objects of MyClass type and store the same content (int value 10) as shown below.

package com.sneppets.corejava;

class MyClass{
	
	int a;
	
	MyClass(int a){
		this.a=a;
	}
}

public class CompareObjects {

	public static void main (String[] args) {
		
                //create two objects of type MyClass and store same content
		MyClass obj1 = new MyClass(10);
		MyClass obj2 = new MyClass(10);
		
		if(obj1.equals(obj2)) {
			System.out.println("obj1 and obj2 are same");
		}else {
			System.out.println("obj1 and obj2 are not same");
		}
		
	}
}

Output

obj1 and obj2 are not same

So in case of MyClass objects (obj1 & obj2), if the references are same then equals() method returns true otherwise it returns false. Here the references are not same so it returned false.

Compare two String Objects using equals()

The following program shows how to compare String objects using equals() method. In this example, let’s create two String objects and store the same content (“Hello“) as shown below.

package com.sneppets.corejava;

public class CompareStringObjects {
	
	public static void main (String[] args) {
		
		String obj1 = new String("Hello");
		String obj2 = new String("Hello");
		
		if(obj1.equals(obj2)) {
			System.out.println("obj1 and obj2 are same");
		}else {
			System.out.println("obj1 and obj2 are not same");
		}
	}

}

Output

obj1 and obj2 are same

Therefore, in case of String objects it compares the contents of the objects. Since the contents are same it returns true. Hence the output is “obj1 and obj2 are same”.

Compare Wrapper Class Objects using equals()

The following program shows how to compare Wrapper class objects using equals() method. In this example, let’s create two Integer objects and store the same content (10) as shown below and check the output.

package com.sneppets.corejava;

public class CompareWrapperClassObjects {
	
	public static void main(String[] args) {
		
		Integer obj1 = new Integer(10);
		Integer obj2 = new Integer(10);
		
		if(obj1.equals(obj2)) {
			System.out.println("obj1 and obj2 are same");
		}else {
			System.out.println("obj1 and obj2 are not same");
		}
	}

}

Output

obj1 and obj2 are same

Therefore, in case of wrapper class (Integer) objects also it compares the contents of the objects similar to String objects. Since the contents are same it returned true. Hence the output is “obj1 and obj2 are same”.

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments