Does a primitive array length reflect the allocated size or the number of assigned elements?
In Java, the length of a primitive array reflects the number of allocated elements in the array. This is the maximum number of elements that the array can hold.
Here is an example of how you can create an array and access its length:
int[] array = new int[5];
System.out.println(array.length); // Outputs: 5
In this example, the array is created with a length of 5, which means it can hold up to 5 elements. The length of the array is not affected by the number of elements that are actually assigned to the array.
For example, you can assign a value to only the first element of the array, and the length will still be 5:
array[0] = 10;
System.out.println(array.length); // Outputs: 5
The length of the array is a property of the array itself, and it cannot be changed once the array is created.
I hope this helps! Let me know if you have any questions.