views:

842

answers:

5

I feel like such a novice for asking this question, but is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this:

public static boolean equals(Object o1, Object o2)
{
    if (o1 == null)
    {
        return o2 == null; // Two nulls are considered equal
    }
    else if (o2 == null)
    {
        return false;
    }

    return o1.equals(o2);
}

It seems silly to write this method myself since I would think that it has to exist already somewhere.

Update: Removed the generics (type parameter) since it did not add any value in this case.

+3  A: 

If you are worried about NullPointerExceptions you could just test equality like:

if (obj1 != null && obj1.equals(obj2)) { ... }

The general contract of equals() is that a non-null object should never be equal to a null reference, and that the equals() method should return false if you are comparing an object to a null reference (and not throw a NPE).

matt b
dcstraw
This is one of those constructs (like checking a string for null and empty or closing a stream without caring about an IOException) that is hard on the eyes and for which commons-lang has a solution for.
SingleShot
I agree with SingleShot and I use commons-lang often for things such as this.
matt b
+9  A: 

Apache Commons Lang has such a method. You don't want generics on such a method, it will lead to bogus compilation errors, at least in general use. Equals knows very well (or should - it is part of the contract) to check the class of the object and return false, so it doesn't need any additional type safety.

Yishai
Yes, I suppose that generics really don't buy you anything in this case. You could still pass in completely different types and the type inference will just select Object as the type parameter. I'm not sure what the "bogus compilation errors" you are referring to are though.
dcstraw
I have definitely seen attempts to do generics like that have issues (with generic parameters, and the like). I couldn't quickly reproduce it in this scenario, so you may be right, but if so, then the generics are pointless, as they will accept anything, so why not just declare Object?
Yishai
Agreed. Using Object is better in this case.
dcstraw
Generics are useless in this case.
Steve Kuo
+2  A: 

Whenever I come across a need and think "this is so common Java must have it" but find it doesn't, I check the Jakarta Commons project. It almost always has it. A quick search of the commons-lang API (which has the most basic of common utilities) shows an equals() method that provides what you want.

SingleShot
A: 

As far as I have seen (in java, c# etc.) you usually use equals in the manner: obj1.equals(obj2) and not SomeLibrary.equals(obj1, obj2)

This is because it is much more scalable for a language and library to rely on an object to know if it equals another object. (as you use obj1.equals anyway) and most of the time if you don't have something to compare to you're in pretty big trouble.

Matt's answer is the best format in my opinion because it doesn't requre another library method call (for what it's worth) and is still intent revealing.

CodePartizan
Matt's answer does not solve the problem. Two null objects being compared should yield true.
SingleShot
I am relying on the object to know if it is equal to another, but I am also adding a null check on top of that. As an aside, you could implement it like that in C# using an extension method on Object, but I've always thought that it is strange when an extension method can be called on a null object without throwing an exception. I prefer a static helper method in this case.
dcstraw
CodePartizan
+1  A: 

Jakarta Commons Lang API has what you are looking for ObjectUtils.equals(Object,Object)

cjstehno
Guess I didnt hit Enter fast enough :-)
cjstehno