If arr is an array of size 10, in the following code block, how many times is arr.length accessed?
for (int i = 0; i < arr.length; ++i);
Once? Or every time it loops?
Thanks everyone! Here’s what I ended up doing:
final int len = arr.length;
for (int i = 0; i < len; ++i);
Since you are permitted to change
arrwithin the loop, it’s usually evaluated every time the loop termination condition is evaluated.An optimising compiler (or JIT) may recognise that you don’t change
arrwithin the loop, and then only evaluatearr.lengthonce.