preface
This article looks at the syntax sugar of Java advanced for loop from the bytecode level, using bytecode analysis, uncover the veil of advanced for loop.
Call for(Object o: List) or for(Object o: Object[])
四, Java for(Object o:Object[])
Let’s write a simple code and look at the bytecode:
/ / Java code
public static void main(String[] args) {
Object [] numbers = new Object[]{};
for(Object i:numbers){ System.out.println(i); }}/ / bytecode
public static void main(java.lang.String[]);
Code:
// Generate an array of length 0 and place it in the first position of the local variable table
0: iconst_0
1: anewarray #2 // class java/lang/Object
4: astore_1
// Make a copy of the array reference and place it in the second position of the local variator, and calculate the array length and place it in the third position of the local variator
5: aload_1
6: astore_2
7: aload_2
8: arraylength
9: istore_3 // Store the length of the array
// Load 0 to the top of the stack, which represents the index, or I, and store it in the fourth position of the local variable table
10: iconst_0
11: istore 4
// Load I and array lengths for comparison
13: iload 4 // load index I
15: iload_3 // Load the array length
16: if_icmpge 39 // If I >= array length jumps to 39 lines, otherwise proceed down
19: aload_2 // Load the array to the top of the stack
20: iload 4 // Load index to top of stack
22: aaload // Load the value of an index in the array
23: astore 5 // An index object in the array is stored in the local variable table
25: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
28: aload 5
30: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;) V
33: iinc 4.1 // change the index value to +1
36: goto 13 // Skip to line 13 and continue
39: return
}
Copy the code
For (int I, I < length; for(Object o: Object[]) I++), the previous advanced form is just Java syntactic sugar
二, Java for(Object o: List)
The code is still simple
//java
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
for(Object o:list){ System.out.println(o); }}/ / bytecode
public static void main(java.lang.String[]);
Code:
// Generate a list and store it in the first location of the local variable table
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
// Get the list iterator into the second position of the local variable table
8: aload_1
9: invokeinterface #4.1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
14: astore_2
// Load the Iterator and execute hasNext, if not, go to line 41, if yes, continue
15: aload_2
16: invokeinterface #5.1 // InterfaceMethod java/util/Iterator.hasNext:()Z
21: ifeq 41
// Get the object from iterator.next and print it, then skip to line 15 to continue the loop
24: aload_2
25: invokeinterface #6.1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
30: astore_3
31: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
34: aload_3
35: invokevirtual #8 // Method java/io/PrintStream.println:(Ljava/lang/Object;) V
38: goto 15
41: return
Copy the code
We can see that the list for loop for(Object O: list), the underlying implementation of the list iterator form