2012-06-29 53 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

相關問題