There are similar questions :
Advanced for loop does cache reference because it's using the same iterator instance : https://stackoverflow.com/a/29812538/4087512
Normal for loop does cache length : https://stackoverflow.com/a/1208331/4087512
But I'm trying to see if the reference is cached in a classic for-loop :
for(int i=0; i<someObject.getPrivateArray().length; i++){
//do something where it's necessary to use i AND getPrivateArray()
}
as opposed to :
int j=0;
for(int var : someObject.getPrivateArray()){
//do something with j & var
j++;
}
From the question answered here : Is there a performance difference between a for loop and a for-each loop? these 2 would have the same performance on an array declared in a local scope, but will there be an overhead of fetching the array from a different object for the for-loop? It was answered above that the array is actually cached in the foreach loop.