Java 1.5 varargs

Published: Tuesday, 22 August 2006

Variable Arguments

I think of this as the same as C style programming. The varargs must be the last argument of a method signature. Instead of receiving an argc, argv, you get an Object[].

Warning: non-varargs call of varargs method with inexact argument type for last parameter

I’ve seen compiler warnings when a null is passed to vararg methods.

Warning:line (48)non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
cast to java.lang.Object[] for a non-varargs call and to suppress this warning

If you pass in (Object) null, then the argument is equivalent to new Object[]{null}. Which is an Object array of length 1. The contents of the first element is null. This means that you cannot do a simple null check on the argument.

If you pass in (Object[]) null, which is the default behaviour, then the Object array is null.