Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null = null is false :confused:

 
Raghu Arikeri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is null = null is false and null <> null is also false?
 
shankar reddy
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null means it is not pointing to no object. null == null , means it gives false ,beacause both are not pointing to same object, even objects are not ther while comparing.
 
Sridhar Padala
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

null==null prints true, because eventhough '==' comparator compares two object references rather tha content, here there is no object as such.So it will print True.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sridhar Padala:
null==null prints true...


I assume we're discussing null=null in an SQL context, not Java.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from Head First SQL

Head First: Calm down and let me get this
straight. You aren�t equal to zero, you aren�t an
empty string variable. And you aren�t even equal to
yourself ? That makes no sense!

NULL: I know it�s confusing. Just think of me
this way: I�m undefined. I�m like the inside of an
unopened box. Anything could be in there, so you
can�t compare one unopened box to another because
you don�t know what�s going to be inside of each
one. I might even be empty. You just don�t know.

You should take a look at Head First SQL. As anything could be there, so null == null returns false, the same with null <> null.
 
Joe Harry
Ranch Hand
Posts: 10090
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really understood the concept of null from the Head First SQL. A database null is like a closed box and you never know what's going to be inside in the future and you cannot compare a closed box with another closed box.
 
Lynn Beighley
author
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quote, Jothi. That's what we try to do throughout these books, explain with metaphors and illustrations to make what seems to be confusing very clear.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
I really understood the concept of null from the Head First SQL. A database null is like a closed box and you never know what's going to be inside in the future and you cannot compare a closed box with another closed box.


thanks jothi and Louis for sharing this, yes it looks much easier to understand the concept. :thumb:
 
Joe Harry
Ranch Hand
Posts: 10090
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lynn,

That was an amazing piece of example to really understand what null is in a database.
 
Campbell Ritchie
Sheriff
Posts: 48427
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But nobody has told him what you ought to write.

If I remember correctly, null in Java means something which doesn't exist, and null in SQL means more like the English "don't know." A bit like RM Reddy's birthday. I don't know when it is, so if I were to put RM Reddy into a database with a BIRTHDAY column, I would have to put null in it. Now I would also put null if I entered Shankar Reddy Telukutla's birthday. Remember this is SQL not Java.
Now both values are recorded as null, but if you ask "do these people have the same birthday?" the answer is "don't know" which SQL expresses as null.

I think there is a standard SQL operator something like is-null, but you will have to go through an SQL tutorial like this one (go to Data statements then Where then look for null) and find out what it says there.

BTW: In Java (null == null) returns true.
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
But nobody has told him what you ought to write.

If I remember correctly, null in Java means something which doesn't exist, and null in SQL means more like the English "don't know." A bit like RM Reddy's birthday. I don't know when it is, so if I were to put RM Reddy into a database with a BIRTHDAY column, I would have to put null in it. Now I would also put null if I entered Shankar Reddy Telukutla's birthday. Remember this is SQL not Java.
Now both values are recorded as null, but if you ask "do these people have the same birthday?" the answer is "don't know" which SQL expresses as null.

I think there is a standard SQL operator something like is-null, but you will have to go through an SQL tutorial like this one (go to Data statements then Where then look for null) and find out what it says there.

BTW: In Java (null == null) returns true.


In SQL

NULL = NULL is false
NULL != NULL is false.

It is not comparative.

The SQL operator you're thinking of for testing for Null is "IS NULL" and "IS NOT NULL". You can also use the COALESCE function to convert a null column to something comparative: COALESCE(column,substitutionValue)

0 = COALESCE(columnOfZeros, 0) is True

I skimmed through Lynn's book in Safari's online books... I wouldn't hesitate in suggesting it to someone wanting to learn SQL. It seems very complete and a really good start to building your knowledge in the subject.
[ October 19, 2007: Message edited by: Paul Campbell ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic