Why int[] a = new int[1] instead of just int a?

后端 未结 6 1464
无人及你
无人及你 2021-02-09 11:55

Is there some hidden meaning in this code which I don\'t see in java? How can it be useful?

int[] a = new int[1];

than just

int         


        
6条回答
  •  萌比男神i
    2021-02-09 12:42

    An array of size one is not the same thing as a single integer.

    Even if they carry the same information, they are different types, so you can use them in different contexts.

    For example, if you have a function which performs a function on all elements of an array but you want to compute it only on one value, you should pass a int[1], because the function expects an array and wants to know how many values it should process.

提交回复
热议问题