File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes null = null is false  :confused: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "null = null is false  :confused: " Watch "null = null is false  :confused: " New topic
Author

null = null is false :confused:

Raghu Arikeri
Greenhorn

Joined: Jan 09, 2006
Posts: 15
why is null = null is false and null <> null is also false?


Raghu
"Work for a cause and not applause. Live to express and not to impress"
shankar reddy
Ranch Hand

Joined: Jun 04, 2007
Posts: 71
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.


Java Lover<br /> <br />Shankar Reddy <br />SCJP1.4 (88%)
Sridhar Padala
Greenhorn

Joined: Aug 03, 2007
Posts: 20
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 28, 2005
Posts: 430
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.


To be or not to be. It's a question.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Lynn Beighley
author
Ranch Hand

Joined: Sep 12, 2007
Posts: 82
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.


Lynn Beighley<br />Author, Head First SQL
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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:


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

Lynn,

That was an amazing piece of example to really understand what null is in a database.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
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

Joined: Oct 06, 2007
Posts: 338
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null = null is false :confused:
 
Similar Threads
keyword
Null
null values in EL doubt.
reserved words
Help me in EL expression question HFSJ Book page 393