3

I am quite confused as to how Java calculates the length of the varargs:

static void hello(Integer... x){

    System.out.println(x.length);
}


public static void hi(){
    hello();
}

This prints a 0.

When I pass :

static void hello(Integer... x){

    System.out.println(x.length);
}


public static void hi(){
    hello(null);
}

This throws a Null pointer exception.

And

static void hello(Integer... x){

    System.out.println(x.length);
}


public static void hi(){
    Integer[] xIntegers= new Integer[44];
    hello(xIntegers);
}

This prints 44.

Can someone please help me understand?

Raj
  • 203
  • 1
  • 2
  • 8

4 Answers4

3

Writing this:

static void hello(Integer... x){

is basically a fancy way of writing this:

static void hello(Integer[] x){

with the added bonus that you can invoke the former in this way:

hello(Integer.valueOf(1), Integer.valueOf(2));

instead of

hello(new Integer[]{Integer.valueOf(1), Integer.valueOf(2)});

Having said that, you can still use the second form with method with varargs.

The second issue at hand is that an array, even an array of primitives, is treated as an object in Java. So if you write:

hello(null);

you are invoking hello with a parameter that is a null array of Integers, and not new Integer[]{null}.

The first call passes an empty array, so the length is 0. Similarly, the third call passes an array of length 44, and you get that as the result.

SeverityOne
  • 2,476
  • 12
  • 25
2

If you pass a non null integer array to your hello() method, then as you have seen you can access the length without any problem. You should therefore check for null input and equate that to an array of length zero:

static void hello(Integer... x) {
    int length = x == null ? 0 : x.length;
    System.out.println(length);

    if (x == null) return;

    // otherwise do something
}

public static void hi() {
    hello(null);
}

Note that values are just passed as an array, and you would have the same problem with the following version of hello():

static void hello(Integer[] x) { }

In this case, if you also tried to read the length of a null input, you would get an exception.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

if the reason is that every array has length if its content has value, remember that each position of the array is a variable of the same type as its container so if you send null the array is empty where you need to validate with an if it is null print null

static void hello(Integer... x){
  if(x!=null)
   System.out.println(x.length);
  else
    System.out.println('is null');
}
nelson E
  • 11
  • 2
0

With a declaration of

static void hello(Integer... x)

You can call it by either passing exactly one Integer[] (which becomes the x inside the method), or passing zero or more Integers (and the compiler will create an Integer[] out of that and that becomes the x inside the method).

In the case of

hello(null)

it's ambiguous whether you are trying to pass a null value of type Integer[], or whether you are trying to pass a single null value of type Integer (both would be legal). It defaults to the former (null value of type Integer[]), and thus x inside the method is null, and not an array of a single null element. Trying to access .length on null causes an exception.

If you want to pass a single null Integer, you could resolve the ambiguity by explicitly casting it to Integer:

hello((Integer)null);

or assign it to a variable of type Integer first:

Integer x = null;
hello(x);

These will print a length of 1.

newacct
  • 119,665
  • 29
  • 163
  • 224