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

前端 未结 2 1242
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: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.

提交回复
热议问题