Difference between int[] array and int array[]

前端 未结 25 3487
轻奢々
轻奢々 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 08:02

    Both are equally valid. The int puzzle[] form is however discouraged, the int[] puzzle is preferred according to the coding conventions. See also the official Java arrays tutorial:

    Similarly, you can declare arrays of other types:

    byte[] anArrayOfBytes;
    short[] anArrayOfShorts;
    long[] anArrayOfLongs;
    float[] anArrayOfFloats;
    double[] anArrayOfDoubles;
    boolean[] anArrayOfBooleans;
    char[] anArrayOfChars;
    String[] anArrayOfStrings;
    

    You can also place the square brackets after the array's name:

    float anArrayOfFloats[]; // this form is discouraged
    

    However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

    Note the last paragraph.

    I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices.

提交回复
热议问题