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

What can be null & how

Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
I often set a primitive or object to an initail value of null, then try to test if it has received a "better" value yet.
I want to write something like

Most of the time the compiler complains about my structuring the first
10x I try to write the loop test.
Once I figure out how to do it, months go by and the next time I can't remember how it worked.
Can someone please tell me what kinds of values (int, String, etc.) can be set to null and tested for a null value?
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
To compare objects you need to use the .equals method. Ususally when I am checking for null, I am checking a string so I go like this:

That seems to work for me. I do not clearly remember why I started doing it this way, but I think that I started doing it that way because I read an article or something that said that null is not considered an object in java.
Lisa M.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Lisa -- -- Sorry, but you should use "==" to compare against null, and the String "null" is absolutely not the same as the literal value null. Have a look at the rest of this answer.
Elouise -- please ignore the previous answer!
The only thing wrong with your code is here

The code in doSomething() is correct and is the right thing to do.
Now, as far as what kinds of values need to be tested for null: objects references do, primitives don't. The primitive types in Java are byte, short, char, int, long, float, double, and boolean. Strings aren't primitives, they're objects, as is everything else not on that list.


[Jess in Action][AskingGoodQuestions]
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
My code was unclear:
all of that code snippet takes place inside class SomeObject. That's why I had a constructor.
Every instance of "SomeObject" has its very own instance of an Object called "nextObj". My intention was to build a singlely linked list of Object (class) objects.
My method "doSomething" needs to test whether nextObj is (still) null so I know whether there are more elements in my list.
What I'd like to check on and finally get down in writing is this:
Can a primitive be given/be tested for a value of null?
If so, how would you write the code?
Can an object be given/be tested for a value of null?
If so, how would you write the code?
[ November 04, 2003: Message edited by: Elouise Kivineva ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What I'd like to check on and finally get down in writing is this:
Can a primitive be given/be tested for a value of null?

No
Can an object be given/be tested for a value of null?
Yes.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What can be null & how