I have seen people say to cache the values of size
for a list or length
for an array when iterating, to save the time of checking the length/size over and over again.
So
for (int i = 0; i < someArr.length; i++) // do stuff
for (int i = 0; i < someList.size(); i++) // do stuff
Would be turned into
for (int i = 0, length = someArr.length; i < length; i++) // do stuff
for (int i = 0, size = someList.size(); i < size; i++) // do stuff
But since Array#length
isn't a method, just a field, shouldn't it not have any difference? And if using an ArrayList, size()
is just a getter so shouldn't that also be the same either way?