aspose file tools*
The moose likes Beginning Java and the fly likes null and not null in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null and not null in java" Watch "null and not null in java" New topic
Author

null and not null in java

Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
Hi,
I've a simple object class as testObj.java with getter and setter methods.
In my code i need to check if the testObj object is null.

My code is :
testObj test = new testObj();
//invoke a method A which returns test as return type
if(test==null || test.equals(""))
{
System.out.println("The test object is null);
}

But i'm not getting the expected results. How to check if this test object is null?

One more question?
what is the different between null and not null in java?

From:Behalf of Premila Devi
Name: Yong Mei Ling
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

To check Object is null or not use


Life is easy because we write the source code.....
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

Premila,

Since this question is not related to servlets, I'm moving the thread to a better forum.

Premila Devi Dayalan wrote:
From:Behalf of Premila Devi
Name: Yong Mei Ling


Hmm... what does that mean?

[My Blog] [JavaRanch Journal]
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
to check whether an object is null use objRef == null
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160


have you tried it???

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
You are confusing a null String and the empty String "". The two are different. You will of course only get true from that equals("") call if test is a String "".

You can always test whether an object is null with x == null or x != null and ought not to use the equals method, which will throw an Exception if called on a null reference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Code tags for code, please Manish Singh, not quote tags.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

test.equals(null) will (or at least should) always return false. And that makes sense - if test is null that code will throw a NullPointerException.

To compare against null, always use "== null" or "!= null". Everything else should be avoided.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
a reference having a value null means that it doesn't point to any object.it is simply declaring a refernce variable.but "not null" means that the refernce is pointing to some object and stores "a memory address about how to get to the object in the memory"
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
Thanks.

From: Chan Mei Ling
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
You're welcome

I don't know whether you have seen this recent thread, which looks at a similar problem from a different angle.
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
Dear Sir,

NOT NULL Example :
"not null" means that the refernce is pointing to some object and stores "a memory address about how to get to the object in the memory"





and



Null Example :
a reference having a value null means that it doesn't point to any object.it is simply declaring a refernce variable






But here,it is "pointing to some object as well"

Chan Mei Ling
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Premila Devi Dayalan wrote:But here,it is "pointing to some object as well"
No, it isn't. A reference either points to an object or it points to null. It can't point to both simultaneously. Your == null test looks whether that reference points to an object or not (== null), and the if-then-else block uses that result to decide which option to take.

And yes, it is called if-then-else, even though Java uses {} instead of the "then."
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40

Dear Sir,

if (asset.getAssetModel().getId() == null) {
assetModelService.add(asset.getAssetModel());
} else {
asset.setAssetModel(assetModelDao.findById(asset.getAssetModel().getId()));
}

So,at these situation,it will check wherever,the (asset.getAssetModel().getId() == null),and **where it will check the **NULL Conditions,is it in the database,refering to **asset.getAssetModel().getId() method,or etc ? How it says/Check "Null"?

Chan Mei Ling
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
What has "Null" got to do with it? You are looking for null, not "Null", nor NULL. Whatever is returned from that method call might be a primitive (in which case you would suffer a compiler error) or an object reference or null. No other possibilities. You are checking whether the reference pointed to by that method call and the reference (probably 0) pointed to by null are the same. That checking, the way you have written it, is done by the JVM looking at heap memory.

Beware of looking in databases, where one uses three-value logic and NULL may be interpreted "don't know", so 123 < NULL, 123 > NULL and 123 = NULL always return NULL. SQL is different from Java, and NULL in SQL may mean something completely different from null in Java. You may need to use the isNull() method when retrieving anything from a database to see whether it was NULL i nSQL.
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
Code tags for code, please Manish Singh, not quote tags.


Sure. Thanks for pointing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
Manish Singh wrote:Sure. Thanks for pointing
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
Dear Sir,

Null fields in a database (bad design practice).Specifiying a field as NOT NULL means when you insert a new record you will always have a value to fill that column. It allows the database system to optermise your database and as NOT NULL are used for primary keys for the database,but programmatically [JAVA] ~ Confused :

Example:



Please correct me,if wrong.

Programmatically,at here we are checking the asset.getAssetModel().getId() == null whether the reference pointed to by that method call and the reference (probably 0) pointed to by null are the same. That checking, the way you have written it, is done by the JVM looking at heap memory.

and basically at my code, asset.getAssetModel().getId() is getting the asset_model_id from the database.At here,are it checking,whether asset_model_id is null or not null? Please advise

Thanks.

Chan Mei Ling

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
You seem to have quoted my post without quote tags. Please use the quote tags so people can distinguish what I wrote from what you wrote.

Yes, the first == null test looks to see whether whatever that method returns is null or is somewhere on the heap. [In Java6 some objects can be created and deleted from the stack, but that doesn't make any difference to the principle.] If it is null you choose the first option in your if-then-else, otherwise you choose the second option (after the else). Since you didn't write "== null" or "!= null" in the else block, no, there is no checking whether anything is null.

When I said "beware" I didn't mean that NULL values in a database were bad; there are instances where they must be NULL, for example when my middle name is entered in a database. What I meant was that you must remember that NULL in SQL and null in Java™ mean different things, although I take your point that database design is often better if you can write NOT NULL.
Premila Devi Dayalan
Ranch Hand

Joined: Jul 19, 2009
Posts: 40
thank a lots.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null and not null in java