tags:

views:

563

answers:

6

Hello,

I am new bie to java unable to check for null.

can you enlighten me on this.

I have int array which has no elements

I tried this code

int[] k = new int[3];

if(k==null)
{
    System.out.println(k.length);
}

but this condition always stay false and nerver prints "k.length"

+14  A: 

There's a key difference between a null array and an empty array. This is a test for null.

int arr[] = null;
if (arr == null) {
  System.out.println("array is null");
}

"Empty" here has no official meaning. I'm choosing to define empty as having 0 elements:

arr = new int[0];
if (arr.length == 0) {
  System.out.println("array is empty");
}

An alternative definition of "empty" is if all the elements are null:

Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    empty = false;
    break;
  }
}

or

Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
  if (ob != null) {
    empty = false;
    break;
  }
}
cletus
+1, couldn't be said better
medopal
ups, the last snippet has `obj !- null`, probably meant to be `obj != null`
Carlos Heuberger
A: 

An int array without elements is not necessarily null. It will only be null if it hasn't been allocated yet. See this tutorial for more information about Java arrays.

You can test the array's length:

void foo(int[] data)
{
  if(data.length == 0)
    return;
}
unwind
A: 

I am from .net background. However, java/c# are more/less same.

If you instantiate a non-primitive type (array in your case), it won't be null.
e.g. int[] numbers = new int[3];
In this case, the space is allocated & each of the element has a default value of 0.

It will be null, when you don't new it up.
e.g.

int[] numbers = null; // changed as per @Joachim's suggestion.
if (numbers == null)
{
   System.out.println("yes, it is null. Please new it up");
}
shahkalpesh
In Java that won't compile, because it will tell you that `numbers` has not been initialized yet. "Uninitialized" and `null` are not the same thing.
Joachim Sauer
Thanks Joachim. I will edit the post to have `int[] numbers` changed to `int[] numbers == null`; In c#, it is not the case.
shahkalpesh
+2  A: 

Look at its length:

int[] i = ...;
if (i.length == 0) { } // no elements in the array

Though it's safer to check for null at the same time:

if (i == null || i.length == 0) { }
Mike
I used this one also. I think it is good coding practice
exiter2000
A: 

An int array is initialised with zero so it won't actually ever contain nulls. Only arrays of Object's will contain null initially.

objects
what if I have to check null for integer
Ankit Sachan
You can't check for null with primitives such as int.
objects
depends where you declared it, if as a class member, then yes it's get initialized with zeroes. but when declared locally inside a method, i believe it's another case... you have to assign an initial value yourself. i suppose. just a thought!
ultrajohn
A: 

I believe that what you want is

int[] k = new int[3];

if (k != null) {  // Note, != and not == as above
    System.out.println(k.length);
}

You newed it up so it was never going to be null.

vickirk