I need to be able to compare two variables, which might at some times be null. If they're both null, I still want it to be considered equal. Is there any appropriate way to do this?
I was testing it out in NetBeans, and looking at the documentation for .equals(), and saw that null references behave weirdly.
These are my examples and results:
Object a = null, b = null;
System.out.println(a == b);
returns true.
OR:
System.out.println(a.equals(b));
throws NullPointerException.
Does == work how I want it to in this case, or am I just 'getting lucky' here and it came to the conclusion 'true' for some other reason?
The NullPointerException is also confusing me, as I always understood Object's equals method to be a reference equality. The documentation repeatedly states 'non-null' Objects, but why would that matter for a reference comparison? Any clarification would be greatly appreciated.
null
. You need to start differentiating between variables, references, and objects.==
.equals
on null does not make sense, because it means these 2 instances are the same in a functional sense