The traditional way to iterate over an array of elements (integer, in this example) is as follows:
int[] array = {5, 10, 15}; for(int i = 0; i < array.length; i++) [
However, does this mean that after each iteration, "array.length" is reevaluated? Wouldn't it be more effective?
int[] array = {5, 10, 15}; int noOfElements = array.length; for(int i = 0; i < noOfElements; i++) {
Thus, (as far as I know), the program only needs to calculate it, and then look at the value of the noOfElements variable.
Note. I know the extended for loop, but you can't use it if you want to use a variable that increments ("i" in this example) to achieve other things inside the loop.
I suspect that in fact it is about whether the Java compiler has the ability to realize that "array.length" does not change and actually reuses this value after calculating it once.
So my question is: Is there a difference in the execution efficiency of the first block of code that I wrote, and the second?
What I get from the answers below is that when creating an instance of the array (is that the right word?), An instance variable called length is created and it is equal to the number of elements in the array.
This means that the operator array.length has nothing to do with the calculation; it refers only to an instance variable.
Thanks for the input guys!