I found this answer on Stack Overflow:
length is constant which is used to find out the array storing capacity not the number of elements in the array
Example:
int a[] = new int[5]
a.lengthalways returns 5 which is called the capacity of an array, so length always returns the CAPACITY. but"number of elements in the array is called size"
Example:
int a[] = new int[5] a[0] = 10Will result in
a.size = 1anda.length = 5.
size()works with collection,lengthworks with arrays in java
(Difference between size and length methods? , 2017-09-06)
As this answer received five upvotes I thought it is correct. But there is actually no size attribute for arrays in Java. I know there's a method for ArrayLists. However, how can a.size be equal to one if normal arrays are used? Am I missing something?
sizemethod. The user is just saying that the array's size is 1. And the user specifically mentions that "sizeworks with collection" as in lists. The user is not wrong, maybe a little confusing but not wrong.a.size= 1 anda.length= 5. (Here,ais an array.)