Its something I am unsure about rather than a problem.This code i had encountered during a test I was taking.I am gonna paste the code here now.
static void count(String...obj){
System.out.println(obj.length);
}
public static void main(String str[]){
count(null,null,null);
count(null,null);
count(null);
}
The program run fine and the output is 3 2 and in the last count call it throws a null point exception(obviously enough).Which was the questions in the test by the way.
Anyways,I am not able to understand what kind of function argument is (String…obj).
Can someone help me with it please.
It’s a varargs parameter, which basically allows you to specify multiple arguments and let the compiler create an array for you.
The reason you get an NPE in the last line is that the compiler effectively has the choice between:
and
… and it prefers the latter.
You could force it to use the former conversion by casting the
null: