Array Length in Java
To find length of an array A
you should use the length
property. It is as A.length
, do not use A.length()
its mainly used for size of string related objects.
The length property will always show the total allocated space to the array during initialization.
If you ever have any of these kind of problems the simple way is to run it. Happy Programming!
Arrays are static memory allocation, so if you initialize an array of integers:
int[] intArray = new int[15];
The length will be always 15, no matter how many indexes are filled.
And another thing, when you intialize an array of integers, all the indexes will be filled with "0".
It contains the allocated size, 10
. The unassigned indexes will contain the default value which is 0
for int
.
First of all, length
is a property, so it would be arr.length
instead of arr.length()
.
And it will return 10, the declared size. The elements that you do not declare explicitely are initialized with 0.