No, this is not exactly the same. And it is easily verifiable and imho is not even 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;
I just turned on println () to see something done with the variable and make sure javac is not optimizing it. Obviously, in the larger picture, the difference does not matter, and they can hardly be measurable, but still, not the same code;)
Although I'm not sure what exactly is happening in the second function, so if someone wants to spend time and analyze the code, continue :-)
source share