In many programming job interview this question has been asked. If you are coming from Python programming background(or you are novice Java programmer) then definitely “==” operator will pop-up in your mind. But in Java, “==” may not give you the right result. equals() method is also used to compare the object. Operator “==” and equals() method should be use cautiously while comparing. There are some subtle differences between these two. “==” is used to compare primitive (int, float, boolean etc.) type and object reference If you want to check whether two object references are pointing to the same object or not then you can use “==” operator because in that case java literally compares the object address. If both object address is same then Java interprets that two object references are pointing to the same object.
equals() method is used to compare two different object. equals() method implementation suggests that the both object should be of same kind and all the member variable should have same value then only returns true otherwise returns false. This requirement may change based on the business logic. For example, business logic asks you to consider two employee equal if they have same employeeId. In this case you just compare the employeeId and based on this you take the decision whether two employee object are same or not. When equals() method is overridden then hashCode() should also be overridden in the class.
“==” is a binary operator Java doesn’t support operator overloading and hence “==” can not be overloaded to compare two objects. equals() is a method and it should be overridden in the class definition. If equals() method is not overridden then its parent class method is called if that also doesn’t exist then Object class equals() method is called which just compares the references.

public boolean equals(Object obj) {
    return (this == obj);

You may give the similar example as given below to prove that “==” operator works in comparing the strings. I should warn you that this is not right example. These string1 and string3 are basically pointing to same object. Under the hood JVM figures out that the two constant string are equal and it allocates only one constant object and assigns the object reference to string1 and string3.

public static void main(String[] args) {
	String string1 = "localhost";
	String string2 = "code4reference";
	String string3 = "localhost";
	if (string1 == string3) {
		System.out.println("string1 and string3 are equal");
	} else if (string1 == string2 ) {
		System.out.println("string1 and string2 are equal");
	System.out.println("string1 identityHashcode : " + System.identityHashCode(string1));
	System.out.println("string3 identityHashcode : " + System.identityHashCode(string3));
	System.out.println("string2 identityHashcode : " + System.identityHashCode(string2));

If you add the last three println statement to your code you will notice that the identityHascode are same for string1 and string3 references which means both the references are pointing to same object.

string1 and string3 are equal
string1 identityHashcode : 30911772
string3 identityHashcode : 30911772
string2 identityHashcode : 10883428

If you still don’t believe me then run the Eclipse in debugging mode. You will notice similar variables window where variable and their corresponding values are displayed. If you notice string1 and string3 ids are same which implies what I have said above.

String references pointing to same object

String references pointing to same object

Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find Facebook page here.


no comment untill now

Add your comment now