2012-06-29 54 views
1

可能重复:
Difference between int[] array and int array[]
java - Array brackets after variable nameJAVA:String [] name = {'a','b','x'}和String name [] = {'a','b','x'};

什么之间 字符串[]名称=差{ 'A', 'B', 'X'}和 字符串名称[] = {'a','b','x'}在JAVA中?

+3

没有语义差异 – nullpotent

+0

哇,看看那里接受的答案:'String [] rectangular [] = new String [10] [10];'(shudder ..) – Thilo

+0

没有区别:两者都是无效的Java表达式(一个用'char's初始化的String [])。纠正后,再次没有区别。 –

回答

5

(差不多)完全没有。

你可以在声明之后放置[];没什么区别。

看到Java Specification的这一位。主要区别是,如果声明多个变量实例会发生什么情况。

int a, b[]; // a is an int, b is an array of int 
int[] a,b; // both are arrays 
+2

'String [] name'然而被认为是最好的做法。 – Keppil

1
String[] name={'a','b','x'} ; 
String name[]={'a','b','x'} ; 

这会给一个compile time error因为你是到字符串数组指派字符。

如果

String[] name={"a","b","x"} ; 
String name[]={"a","b","x"} ; 

则都是相同的。

You can put the [] before or after the declaration 
0

没什么。为了测试它,你可以这样做:

public class TestType { 

    public static void main(String[] args) { 
     String[] v1 = {}; 
     String v2[] = {}; 

     System.out.println(v1.getClass() == v2.getClass()); 
    } 

} 

这将打印true

0

这两者之间没有什么区别,两者都是在Java中声明数组的不同方法。我总是用String[] name

相关问题