• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

null and not null in java

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To check Object is null or not use
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to check whether an object is null use objRef == null
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


have you tried it???

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code tags for code, please Manish Singh, not quote tags.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

From: Chan Mei Ling
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Code tags for code, please Manish Singh, not quote tags.



Sure. Thanks for pointing
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish Singh wrote:Sure. Thanks for pointing

 
Premila Devi Dayalan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank a lots.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
reply
    Bookmark Topic Watch Topic
  • New Topic