Should null == null be true when comparing objects?

For general purpose programming, null == null should probably return true I can't count the number of times I've run into the if( obj! = null ) { //call methods on obj } pattern, and it often seems unavoidable. If null == null evaluated to false, this pattern would fall apart, and there wouldn't be a good way to handle this case without exceptions.

For general purpose programming, null == null should probably return true. I can't count the number of times I've run into the if( obj! = null ) { //call methods on obj } pattern, and it often seems unavoidable.

If null == null evaluated to false, this pattern would fall apart, and there wouldn't be a good way to handle this case without exceptions.

In SQL there's a special operator for that called 'is'. So to test if something is NULL you use 'is NULL'. That is also the common practice in Python, although == also works.

– gooli Nov 3 '09 at 5:42 Similarly, in VB. NET you have: Is Nothing – Rob Sobers Nov 3 '09 at 15:06.

I think that null in Java, just like NULL in C++ or None in Python, means specifically "there's nothing here" -- not "I don't know", which is a concept peculiar to SQL, not common in OOP languages.

I agree. In code, we often KNOW we have or get null - nothing. The state is finite and predictable.

– Stefan Kendall Nov 3 '09 at 2:58.

I think you've got your basic facts about SQL completely wrong. NULL is a data value and UNKNOWN is a logical value. NULL = NULL is UNKNOWN.

NULL = NULL is certainly not FALSE! Google for "three value logic". The NULL value is the placeholder for a missing data value.

Ideally, a NULL-able column should only be used for values that are only temporarily missing i.e. There's a reasonable expectation that a non-NULL value will be available in the future e.g. Using the NULL value for the end date in a pair of DATETIME values used to model a period to signify infinity i.e. This period is current (though a far-future DATEITME value works well too).

I am not a SQL guy so that is totally possible. I work mostly with oracle and in oracle NULL + anything = NULL and SELECT 1 from DUAL WHERE NULL=NULL returns 0 rows. I have read that this is because "I don't know" is the conceptual definition of null.

– George Mauer Nov 3 '09 at 16:48 In your defence, I think it is true that Oracle does not implement NULL correctly. IIRC, '' empty string IS NULL is TRUE for Oracle, whereas in SQL it is FALSE. Note that NULL is treated differently for SQL DDL e.g. A row-level CHECK constraint that evaluates to NULL allows the update to succeed, a kind of 'benefit of the doubt' effect.

– onedaywhen Nov 4 '09 at 8:12.

I already get annoyed enough at the IEEE NaN not being equal to itself. I tend to view == as an equivalence relation, one of whose properties is reflexivity. If you have special semantics of "unknown" value equality, I think you should use something more specific rather than overloading an operator whose semantics are well understood.

For example, just because you don't know if two values are equal, doesn't mean they definitely are not equal, which is what I'd guess from a False return value from ==. It seems like you really want some sort of ternary logic. Depending on your language, it may or may not be easy for you to come up with a concise ==-alike that can return True or False or NoClue, but I definitely think it should be separate.

Just my opinion on the matter though :).

Thats true, "I don't know"=="I don't know" should result in "I don't know", not false - though converting from "I don't know" to false implicitly seems ok. – George Mauer Nov 3 '09 at 3:09 2 I think you mean True, False, or FileNotFound ;) – Mike Spross Nov 3 '09 at 3:39.

If you said null === null, I would agree with you.

1 Hmm, thats a good point actually. A really really good point. – George Mauer Nov 3 '09 at 4:27.

First null == null being true makes patterns like if(!(instance == null)) { // do something requiring instance not be null } work. (Yes, the usual test is instance! = null but I want to make clear the use of!(null == null) being false.) Second, if you need instance1 == instance2 to be false when instance1 and instance2 are null reference instances of your class, then this should be encapsulated into a logic class somewhere.

In C#, we would say class MyObjectComparer : IEqulityComparer { public bool Equals(MyObject instance1, MyObject instances2) { if(instance1 == null && instance2 == null) { return false; } // logic here } public int GetHashCode(MyObject instance) { // logic here } }.

All null pointers (or references) are equal to each other. They have to be, otherwise how would you compare a null pointer tonull?

Well in C# at least the correct way of doing null checks is object. ReferenceEquals(null, variable) – George Mauer Nov 3 '09 at 4:01 Is this better (or different) than p == null? – Loadmaster Nov 3 '09 at 16:11.

C++: comparing null pointers always returns true. If somehow you have a null reference (don't do that) the result is crash.

In my opinion the current behavior is correct, especially if you consider that null is interpreted as "Unknown value". Think about it this way: If someone asked you whether the number of apples inside two boxes that you didn't know the contents of were equal. The answer wouldn't be yes or no, it would be "I don't know.

1 Exactly, and that's the way NULL in SQL works, there is just an implicit cast between NULL and false – George Mauer Nov 3 '09 at 4:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions