Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How many times is arr.length called in a for-loop?

Tags:

java

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);
like image 814
Loops Avatar asked Dec 03 '22 04:12

Loops


1 Answers

Eleven times....

10 times, the statement i < arr.length evaluates to true.
1 time it evalutes to false and the loop ends.

like image 58
Felix Kling Avatar answered Dec 26 '22 02:12

Felix Kling