Difference between int[] array and int array[]

前端 未结 25 3003
轻奢々
轻奢々 2020-11-21 07:18

I have recently been thinking about the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]
  3. <
25条回答
  •  悲&欢浪女
    2020-11-21 07:56

    From section 10.2 of the Java Language Specification:

    The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:

     byte[] rowvector, colvector, matrix[];
    

    This declaration is equivalent to:

    byte rowvector[], colvector[], matrix[][];
    

    Personally almost all the Java code I've ever seen uses the first form, which makes more sense by keeping all the type information about the variable in one place. I wish the second form were disallowed, to be honest... but such is life...

    Fortunately I don't think I've ever seen this (valid) code:

    String[] rectangular[] = new String[10][10];
    

提交回复
热议问题