what is the difference between the following 2 loops
for(int i = 0, n = array.length; i < n; i++)
//do something
for(int i = 0; i < array.length; i++)
//do something
does the loop calculate the array length each iteration in the second loop?
The first form only evaluates the expression
array.lengthonce, and remembers it in an extra local variable (n). In most languages I’ve worked with, finding the length of an array is incredibly quick anyway, so the latter form is preferred.