Page 1 of 1

About Question enthuware.ocpjp.i.v11.2.3230 :

Posted: Mon Jul 29, 2019 12:07 pm
by zunayeed
we know that, var is not valid for array declaration. Can you please explain how come does this work ?
var ia = new int[][]{ {1, 2}, null };

thanks

Re: About Question enthuware.ocpjp.i.v11.2.3230 :

Posted: Mon Jul 29, 2019 12:48 pm
by admin
Where did you read that var is not valid for array declaration??

Re: About Question enthuware.ocpjp.i.v11.2.3230 :

Posted: Mon Jul 29, 2019 6:22 pm
by zunayeed
Not allowed with array initializer (an array initializer still needs an explicit target type):

public static void main(String[] args) {
var arr = {1, 2, 3};
}
source: http://java.boot.by/ocpjd11-upgrade-gui ... .html#c7s1
Can you please explain why I get this information wrong ?
thank you

Re: About Question enthuware.ocpjp.i.v11.2.3230 :

Posted: Mon Jul 29, 2019 9:11 pm
by admin
Well, there is a difference between the two. The type is specified explicitly in new int[][]{ {1, 2}, null }; , so compiler can determine the type of the variable. But not in case of {1, 2, 3};. How will the compiler know what type to use for var? It could be short or byte array also.

Also, you might want to check with the author of that article and get his opinion.