为什么Java允许私有String [] [] var [] [英] Why java allows private String[][] var[]
问题描述
private String[][] var[]
此代码进行编译.但是他们不应该将数组放在类型或变量上吗?
This code compiles. But shouldn't they make the array on type or variable?
推荐答案
代码与此相同:
private String[][][] var;
所有这些形式都是等效的:
All these forms are equivalent:
private String[][][] var;
private String[][] var[];
private String[] var[][];
private String var[][][];
它们的意思都一样:三维字符串数组. String var[][][]
语法可能看起来有些怪异,但这就像是使C/C ++程序员在Java中有如家一般的感觉(这就是通常在C/C ++中声明数组类型的方式).
And they all mean the same: a three-dimensional array of strings. The String var[][][]
syntax may seem a bit weird, but it's like that for making C/C++ programmers feel right at home in Java (that's how you normally declare an array type in C/C++).
在Java中,首选String[][][] var
语法,因为它清楚地表明var
的 type 是String[][][]
,在其他语法及其不同的变体中,类型信息被拆分变量之前和之后-尽管从编译器的角度来看这是完全合法的,但很难阅读.
In Java, the String[][][] var
syntax is preferred, as it makes clear that the type of var
is String[][][]
, in the other syntax and its different variations the type information is split before and after the variable - although it's perfectly legal from the compiler's view point, it's harder to read.
甚至更奇怪,所有这些方法声明都是合法且等效的:
Even weirder, all these method declarations are legal and equivalent:
String[][][] m() {return null;}
String[][] m() [] {return null;}
String[] m() [][] {return null;}
String m() [][][] {return null;}
这篇关于为什么Java允许私有String [] [] var []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!