Since it .size()is a method, its implementation is hidden. And you cannot be sure that this method is optimized (it is likely that some collections, for example, list items every time you call the method .size()). This is why calculating a .size()collection can be expensive. At the same time, it .isEmpty()can check if there is at least one element in the collection and return the result.
But .length- this is just a field. It does nothing, just returns a value. There are no calculations. This field simply stores the length of the array with a fixed size.
I believe that .size()and .lengthare completely different things, and in this case they cannot be compared, as well as collections and arrays.
source
share