When overriding equals method for an object can obj below ever be null, if so what are the circumstances?
@Override
public boolean equals(Object obj) {
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes,
equals()should handlenulland in that case it should returnfalseas documented inObject.equals():Of course if
xisnullx.equals(null)won’t returntruebut it will throwNullPointerException. If you need to compare two possiblynullreferences look atObjects.equals(Object, Object):The above is always
true, even ifx == null.