Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What exactlyy does null mean?

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happens all the time! Don't sweat it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic