Array Length in Java

前端 未结 16 1459
忘了有多久
忘了有多久 2020-11-30 21:27

I declared an array as shown below:

int[] arr = new int[10];

Then I assigned following values to the array:

arr[0] = 1;
arr         


        
16条回答
  •  萌比男神i
    2020-11-30 22:02

    In this case, arr.length will return 10, the size of array you allocated. Logical size doesn't really apply here, as this is a fixed length array.

    When you initialize the array:

    int[] arr = new int[10];
    

    Java will create an array with 10 elements and initialize all of these to 0. See the Java language spec for details of initial values for this and other primitive types.

提交回复
热议问题