First 2 examples:
1)
MyClass myClass;
for (int i=0; i<arrayList.size(); i++) {
myClass = arrayList.get(i);
...
}
2)
for (int i=0; i<arrayList.size(); i++) {
MyClass myClass = arrayList.get(i);
...
}
In the first example the reference variable myClass is created only once. But what about in the second example, is it created only once, or once for each iteration? My thought is perhaps the compiler optimizes this, I don't know.
I tried to answer this question by coding an example, but couldn't figure it out. How can it be proven via code?
Note: I realize example 2 is better style since myClass isn't known outside of the for loop, and it's scope is kept to a minimum. I also have searched here but haven't found a definitive answer to this exact question (usually it's a question of "which is preferred?".) I also assume that if the myClass reference is created each iteration it isn't a big performance issue.
Edit: Again, I am not asking which is better coding style. Also, I wonder if it can be deduced/proven via code. I tried to generate and compare the bytecode, but I am not familiar with bytecode and what was generated was not an exact match.