Can two object which are not equal have same hashCode?
YES, two object, which are not equal by equals() method can still return same hashCode. By the way, this is one of the confusing bit of equals and hashcode contract.

How does get() method of HashMap works, if two keys has same hashCode?
This is the follow-up of previous interview questions on equals and hashcode, in fact some time this leads to discussion of the earlier point. When two key return same hashcode, they end-up in same bucket. Now, in order to find the correct value, you used keys.equals() method to compare with key stored in each Entry of linked list there. Remember to point out keys.equals() method, because that's what interviewer is looking for. You can also see here for full list of interview question on Java HashMap.

Where have you written equals() and hashCode in your project?
This is to see, if developer has even written these methods or not. Of course almost all of Java programmer are exposed to this, you can point out value objects, Hibernate entities from your domain, where you have overridden equals and hashCode. Always gives examples from your domain and from your project, rather than a trivial example from a test program, because if Interviewer is asking this question, it means he is interested in examples form your domain.

Suppose your Class has an Id field, should you include in equals()? Why?
This question is asked to one of my reader as Hibernate Interview question, well including id is not a good idea in equals() method because this method should check equality based upon content and business rules. Also including id, which is mostly a database identifier and not available to transient object until they are saved into database.

What happens if equals() is not consistent with compareTo() method?
This is an interesting questions, which asked along with equals() and hashCode() contract. Some java.util.Set implementation e.g. SortedSet or it's concrete implementation TreeSet uses compareTo() method for comparing objects. If compareTo() is not consistent means doesn't return zero, if equals() method returns true, the it may break Set contract, which is not to avoid any duplicates.

What happens if you compare an object with null using equals()?
When a null object is passed as argument to equals() method, it should return false, it must not throw NullPointerException, but if you call equals method on reference, which is null it will throw NullPointerException. That’s why it’s better to use == operator for comparing null e.g. if(object != null) object.equals(anohterObject). By the way if you comparing String literal with another String object than you better call equals() method on literal rather than known object to avoid NPE, one of those simple tricks to avoid NullPointerException in Java.

What is difference in using instanceof and getClass() method for checking type inside equals?
This question was asked multiple times, sometime by looking at your equals() and hashCode implementation. Well key difference comes from point that instanceof operator returns true, even if compared with sub class e.g. Subclass instanceof Super class is true, but with getClass() it's false. By using getClass() you ensure that your equals() implementation doesn't return true if compared with sub class object. While if you use instanceof operator, you end up breaking symmetry rule for equals which says that if a.equals(b) is true than b.equals(a) should also be true. Just replace a and b with instance of Super class and Sub class, and you will end up breaking symmetry rule for equals() method.

How do you avoid NullPointerException, while comparing two Strings in Java?
Since when compared to null, equals return false and doesn't throw NullPointerException, you can use this property to avoid NPE while using comparing String. Suppose you have a known String "abc" and you are comparing with an unknown String variable str, then you should call equals as "abc".equals(str), this will not throw Exception in thread Main: java.lang.NullPointerException, even if str is null. On the other hand if you call str.equals("abc"), it will throw NPE. So be careful with this. By the way this is one of the Java coding best practices, which Java developer should follow, while using equals() method.

What is difference between "==" and equals() method in Java?
One of the most classical interview question on equals(). It has been asked numerous times during in past decade. I have also covered this question already. See here for detailed discussion on how it affect equality checking of String and Integer in auto boxing world.


Kindly Share This Post »»

Responses

0 Respones to "Java Equals() and HashCode() Interview Questions with Answers"

Post a Comment

 
HOME | Freshers | Exp | Java | SQL | Walkins | OffCampus | BankJobs
=*= PRIVACY POLICY And DISCLAIMER =*=
Some of the stuff contained this site are found on internetThis site is not responsible for publishing all available Information as well accuracy, please check posted Information with its original sources, e.g. News Papers/ Websites etc.We collect all the Information form the Internet.
Software My Zimbio
Copyright © 2013 Career+ Blog +Google | Disclaimer | Privacy Policy | Contact