aspose file tools*
The moose likes Beginning Java and the fly likes What exactlyy does null mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What exactlyy does null mean?" Watch "What exactlyy does null mean?" New topic
Author

What exactlyy does null mean?

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I would like to see whether or not an array is empty.

i.e. Vehicle car [ ]

Could I simple say:
if (car [1] != null )

would this mean it is empty?

I am confused. Does null mean zero and if it does what do I use to see if it is empty? i.e. no value stored?
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Null does not mean zero. Your code

if (car [1] != null )

is only checking the one index of the car array.

To check the entire array you'd have to loop through the array checking each index.

Read up on default values for primitives and object to get a better understanding of null. Many times null occurs when something has been declared but not initialized. It's also used to check if something exists, for instance in a web application I often do

if (sessionData != null) {
// proceed
}
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
So to say vehicle [1] is empty I would say:

if (vehicle [1] != null)
{
//Means something is stored in vehicle [1]?
}
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
You don't have an array called 'vehicle', just one called 'car'. You can check the length of an array. If the length is 0, it's empty. Or you can cycle through an array if it's been declared for size but the elements have not been initialized. You use a loop to do this.

Basically, "null" means "no value" or "uninitialized".
[ November 03, 2004: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218


Thank you for clarifying this. I think I was getting confused.
I apologise if I sounded silly.

I was getting confused with the value null and the array reference [0]

I feel rather silly now
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
I think you meant to say

if (car [1] != null)
{

//Means something is stored in vehicle [1]?
}

car is the name of the array, vehicle is the type of object stored in the array according to your original post.

If that's what you meant then yes the code

if (car [1] != null)
{

//Means something is stored in vehicle [1]?
}
checks to see if there is a value in index 1 of the array.

Here's how you would loop through the entire array

String sArray[] = new String[10];
for (int i = 0; i < sArray.length; i++) {
if (sArray[i] != null) {
System.out.println("sArray[i] has a value!");
}
}

Whoops you replied while I was doing this.
[ November 03, 2004: Message edited by: Hentay Duke ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Happens all the time! Don't sweat it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What exactlyy does null mean?
 
Similar Threads
finder specification
Arraylist
How to implement Comparable
alternative to nested if in TreeMap values
HFSJ book - Chapter 8 Q 17