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
About Question enthuware.ocpjp.i.v11.2.3230 :
Moderator: admin
-
- Posts: 10
- Joined: Fri Jun 28, 2019 10:58 am
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3230 :
Where did you read that var is not valid for array declaration??
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Fri Jun 28, 2019 10:58 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3230 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3230 :
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.
Also, you might want to check with the author of that article and get his opinion.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: abcackle, Bing [Bot] and 102 guests