Why does Java allow private string [] [] var []

private String[][] var[]
private String[][] var[]

This code is compiled But shouldn't they create arrays on types or variables?

Solution

The code is the same:

private String[][][] var;

All these forms are equivalent:

private String[][][] var;
private String[][] var[];
private String[] var[][];
private String var[][][];

They are all the same: a three - dimensional array of strings String var [] [] syntax may seem strange, but it's like making C / C + + programmers feel good in Java (that's why you usually declare an array type in C / C + +)

In Java, string [] [] var syntax is preferred because it clearly indicates that the type of VaR is string [] []. In other syntax and its different variants, type information splits variables before and after - although it is completely legal from the compiler's point of view, it is more difficult to read

Even more strangely, all of these method declarations are legal and equivalent:

String[][][] m()  {return null;}
String[][] m() [] {return null;}
String[] m() [][] {return null;}
String m() [][][] {return null;}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>