in array declaration int[] k,i and int k[],i;

前端 未结 2 1243
Happy的楠姐
Happy的楠姐 2020-12-07 03:43

While declaring array we can use brackets any side of the identifier but in the case:

int[] k,i;

and

int k[],i;

相关标签:
2条回答
  • 2020-12-07 04:12

    Think of it like this:

    (int[]) k, i
    

    int (k[]), (i)

    In the first example, the brackets are associated with the int keyword, specifying that you want to create int arrays. In the second, you specify type int, then declare an array of ints k, and an int i.

    0 讨论(0)
  • 2020-12-07 04:36

    My guess is the following:

    The declaration int[] k is more logical, because it declares k to be an array of int. Therefore, it is the preferred (?) style in Java.

    int k[], on the other hand, was the C way of declaring this array (K&R had a different philosophy when it came to declaration syntax – they wanted declarations to mimic access to the variable) and to ease the transition for C programmers, this syntax was also allowed – no harm done.

    Now, in your above statement you have chained two declarations. In the first case, both variables are declared with the same type – which is clearly int[]. However, in the second code this behaviour would be counter-intuitive (and also different from C’s behaviour), and therefore has other semantics.

    Keep in mind that this is purely a guess.

    0 讨论(0)
提交回复
热议问题