No it's not exactly the same. And that's easily verifyable and imho not even that surprising.
Just decompile the following two functions:
public static void test1(Object[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public void test2(Object[] arr) {
for(Object o : arr) {
System.out.println(o);
}
}
and look at the output:
public static void test1(java.lang.Object[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: aload_0
4: arraylength
5: if_icmpge 23
8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
11: aload_0
12: iload_1
13: aaload
14: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
17: iinc 1, 1
20: goto 2
23: return
public void test2(java.lang.Object[]);
Code:
0: aload_1
1: astore_2
2: aload_2
3: arraylength
4: istore_3
5: iconst_0
6: istore 4
8: iload 4
10: iload_3
11: if_icmpge 34
14: aload_2
15: iload 4
17: aaload
18: astore 5
20: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
23: aload 5
25: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
28: iinc 4, 1
31: goto 8
34: return
}
I just included the println() so that we see something's done with the variable and to make sure javac doesn't optimize it away. Obviously in the bigger picture the difference won't matter and they're hardly measureable but still, not the same code ;)
Though I'm not sure what exactly's going on in the 2nd function, so if someone wants to take the time and dissect the code go on ;-)