Java For Loop differences
Are these statements completely the same in terms of memory occupation and efficiency in Java?
First:Object[] array = new Object[100];
int i = 0;
for开发者_运维技巧 (; i < array.length; i++){
Object o = array[i];
//do something
}
Second:
Object[] array = new Object[100];
for (int i = 0; i < array.length; i++){
Object o = array[i];
//do something
}
Third:
Object[] array = new Object[100];
for (Object o : array){
//do something
}
In terms of memory occupation and efficiency, yes. However, there are differences. In the first, i
exists (has a scope) beyond the loop; while in the second it does not. In the third case, there is no (direct) way to access the index or to change the array contents at the position of the current object.
The first one is not the common idiom; I wouldn't write it that way.
There's no memory or efficiency difference. The third is syntactic sugar that was added in a later JVM (I believe it was JDK 6).
Your code will be the memory and efficiency bottleneck, not your loop constructs.
The third version was introduced with Java 5, is designed to simplify your work with generics. It is enhanced because you don't need to determine how many elements are in the array before looping. There is also no need to specify how to increment the current position, providing a cleaner implementation without having to create a counter variable or an Iterator.
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 ;-)
精彩评论