在java裡,我們是怎麼寫for循環的代碼呢。一般都是用這兩種寫法:
第一種:最傳統的for循環寫法,for(代碼段a;代碼段b;代碼段c),其中代碼段a是初次進入for循環時執行的代碼,代碼段b是一個boolean的表達式,true則繼續執行for循環內容,false則停止for循環,代碼段c則是在for循環內部執行完後執行。
第二種:針對集合的遍歷,for(類型 單個對象:集合對象)。第二種即是增強for循環。
本文的目的就是講解增強for循環的原理。先看我的測試用例:
jdk版本:1.7.0_51
java 用例代碼:
package com.onlyou.olyfinance.supply.web; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * 增強for循環編譯測試 * Created by cd_huang on 2017/4/14. */ public class ForeachTest { public static void superForeachTestArray(){ String[] args =new String[]{"1","2"}; for(String s:args) { System.out.println(s); } } public static void foreachTestArray(){ String[] args =new String[]{"1","2"}; String[] args2 = args; int len = args.length; for(int i = 0; i < len; ++i) { String s= args2[i]; System.out.println(s); } } public static void superForeachTestIterator(){ List<String> list = Arrays.asList(new String[]{"1","2"}); for(String s:list) { System.out.println(s); } } public static void foreachTestIterator(){ List<String> list = Arrays.asList(new String[]{"1","2"}); Iterator it = list.iterator(); while(it.hasNext()) { String s = (String)it.next(); System.out.println(s); } } }
命令行輸入:C:\Users\hcd>javac c:\ForeachTest.java
生成StringTest.class文件。
命令行輸入:C:\Users\hcd>javap -v c:\ForeachTest.class
生成java編譯後的字節碼指令。
Classfile /c:/ForeachTest.class Last modified 2017-4-14; size 1307 bytes MD5 checksum 5296942b41a468b4fa1f88e3123db02f Compiled from "ForeachTest.java" public class com.onlyou.olyfinance.supply.web.ForeachTest SourceFile: "ForeachTest.java" minor version: 0 major version: 51 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #13.#28 // java/lang/Object."<init>":()V #2 = Class #29 // java/lang/String #3 = String #30 // 1 #4 = String #31 // 2 #5 = Fieldref #32.#33 // java/lang/System.out:Ljava/io/PrintStream; #6 = Methodref #34.#35 // java/io/PrintStream.println:(Ljava/lang/String;)V #7 = Methodref #36.#37 // java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; #8 = InterfaceMethodref #38.#39 // java/util/List.iterator:()Ljava/util/Iterator; #9 = InterfaceMethodref #40.#41 // java/util/Iterator.hasNext:()Z #10 = InterfaceMethodref #40.#42 // java/util/Iterator.next:()Ljava/lang/Object; #11 = InterfaceMethodref #38.#39 // java/util/List.iterator:()Ljava/util/Iterator; #12 = Class #43 // com/onlyou/olyfinance/supply/web/ForeachTest #13 = Class #44 // java/lang/Object #14 = Utf8 <init> #15 = Utf8 ()V #16 = Utf8 Code #17 = Utf8 LineNumberTable #18 = Utf8 superForeachTestArray #19 = Utf8 StackMapTable #20 = Class #45 // "[Ljava/lang/String;" #21 = Utf8 foreachTestArray #22 = Utf8 superForeachTestIterator #23 = Class #46 // java/util/List #24 = Class #47 // java/util/Iterator #25 = Utf8 foreachTestIterator #26 = Utf8 SourceFile #27 = Utf8 ForeachTest.java #28 = NameAndType #14:#15 // "<init>":()V #29 = Utf8 java/lang/String #30 = Utf8 1 #31 = Utf8 2 #32 = Class #48 // java/lang/System #33 = NameAndType #49:#50 // out:Ljava/io/PrintStream; #34 = Class #51 // java/io/PrintStream #35 = NameAndType #52:#53 // println:(Ljava/lang/String;)V #36 = Class #54 // java/util/Arrays #37 = NameAndType #55:#56 // asList:([Ljava/lang/Object;)Ljava/util/List; #38 = Class #46 // java/util/List #39 = NameAndType #57:#58 // iterator:()Ljava/util/Iterator; #40 = Class #47 // java/util/Iterator #41 = NameAndType #59:#60 // hasNext:()Z #42 = NameAndType #61:#62 // next:()Ljava/lang/Object; #43 = Utf8 com/onlyou/olyfinance/supply/web/ForeachTest #44 = Utf8 java/lang/Object #45 = Utf8 [Ljava/lang/String; #46 = Utf8 java/util/List #47 = Utf8 java/util/Iterator #48 = Utf8 java/lang/System #49 = Utf8 out #50 = Utf8 Ljava/io/PrintStream; #51 = Utf8 java/io/PrintStream #52 = Utf8 println #53 = Utf8 (Ljava/lang/String;)V #54 = Utf8 java/util/Arrays #55 = Utf8 asList #56 = Utf8 ([Ljava/lang/Object;)Ljava/util/List; #57 = Utf8 iterator #58 = Utf8 ()Ljava/util/Iterator; #59 = Utf8 hasNext #60 = Utf8 ()Z #61 = Utf8 next #62 = Utf8 ()Ljava/lang/Object; { public com.onlyou.olyfinance.supply.web.ForeachTest(); flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 11: 0 public static void superForeachTestArray(); flags: ACC_PUBLIC, ACC_STATIC Code: stack=4, locals=5, args_size=0 0: iconst_2 1: anewarray #2 // class java/lang/String 4: dup 5: iconst_0 6: ldc #3 // String 1 8: aastore 9: dup 10: iconst_1 11: ldc #4 // String 2 13: aastore 14: astore_0 15: aload_0 16: astore_1 17: aload_1 18: arraylength 19: istore_2 20: iconst_0 21: istore_3 22: iload_3 23: iload_2 24: if_icmpge 46 27: aload_1 28: iload_3 29: aaload 30: astore 4 32: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 35: aload 4 37: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 40: iinc 3, 1 43: goto 22 46: return LineNumberTable: line 14: 0 line 15: 15 line 16: 32 line 15: 40 line 18: 46 StackMapTable: number_of_entries = 2 frame_type = 255 /* full_frame */ offset_delta = 22 locals = [ class "[Ljava/lang/String;", class "[Ljava/lang/String;", int, int ] stack = [] frame_type = 248 /* chop */ offset_delta = 23 public static void foreachTestArray(); flags: ACC_PUBLIC, ACC_STATIC Code: stack=4, locals=5, args_size=0 0: iconst_2 1: anewarray #2 // class java/lang/String 4: dup 5: iconst_0 6: ldc #3 // String 1 8: aastore 9: dup 10: iconst_1 11: ldc #4 // String 2 13: aastore 14: astore_0 15: aload_0 16: astore_1 17: aload_0 18: arraylength 19: istore_2 20: iconst_0 21: istore_3 22: iload_3 23: iload_2 24: if_icmpge 46 27: aload_1 28: iload_3 29: aaload 30: astore 4 32: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 35: aload 4 37: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 40: iinc 3, 1 43: goto 22 46: return LineNumberTable: line 21: 0 line 22: 15 line 23: 17 line 24: 20 line 25: 27 line 26: 32 line 24: 40 line 28: 46 StackMapTable: number_of_entries = 2 frame_type = 255 /* full_frame */ offset_delta = 22 locals = [ class "[Ljava/lang/String;", class "[Ljava/lang/String;", int, int ] stack = [] frame_type = 250 /* chop */ offset_delta = 23 public static void superForeachTestIterator(); flags: ACC_PUBLIC, ACC_STATIC Code: stack=4, locals=3, args_size=0 0: iconst_2 1: anewarray #2 // class java/lang/String 4: dup 5: iconst_0 6: ldc #3 // String 1 8: aastore 9: dup 10: iconst_1 11: ldc #4 // String 2 13: aastore 14: invokestatic #7 // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; 17: astore_0 18: aload_0 19: invokeinterface #8, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 24: astore_1 25: aload_1 26: invokeinterface #9, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 31: ifeq 54 34: aload_1 35: invokeinterface #10, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 40: checkcast #2 // class java/lang/String 43: astore_2 44: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 47: aload_2 48: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 51: goto 25 54: return LineNumberTable: line 31: 0 line 32: 18 line 33: 44 line 34: 51 line 35: 54 StackMapTable: number_of_entries = 2 frame_type = 253 /* append */ offset_delta = 25 locals = [ class java/util/List, class java/util/Iterator ] frame_type = 250 /* chop */ offset_delta = 28 public static void foreachTestIterator(); flags: ACC_PUBLIC, ACC_STATIC Code: stack=4, locals=3, args_size=0 0: iconst_2 1: anewarray #2 // class java/lang/String 4: dup 5: iconst_0 6: ldc #3 // String 1 8: aastore 9: dup 10: iconst_1 11: ldc #4 // String 2 13: aastore 14: invokestatic #7 // Method java/util/Arrays.asList:([Ljava/lang/Object;)Ljava/util/List; 17: astore_0 18: aload_0 19: invokeinterface #11, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 24: astore_1 25: aload_1 26: invokeinterface #9, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 31: ifeq 54 34: aload_1 35: invokeinterface #10, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 40: checkcast #2 // class java/lang/String 43: astore_2 44: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 47: aload_2 48: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 51: goto 25 54: return LineNumberTable: line 38: 0 line 39: 18 line 40: 25 line 41: 34 line 42: 44 line 43: 51 line 44: 54 StackMapTable: number_of_entries = 2 frame_type = 253 /* append */ offset_delta = 25 locals = [ class java/util/List, class java/util/Iterator ] frame_type = 28 /* same */ }
上面的是java代碼編譯過後的二進制指令。如果對這些指令是什麼意思有興趣的可以看這篇博客:http://www.blogjava.net/DLevin/archive/2011/09/13/358497.html。不過單單看這個肯定是不夠的。需要對java的運行時內存模型有一定的了解,明白棧幀,操作數棧,局部變量表,常量池這些東西的概念,才能理解在執行這些指令時,對應的東西裡會產生什麼變化。當然,要理解我說的增強for循環原理,並不需要看懂這些指令。認真觀察上面的二進制指令,superForeachTestArray()方法編譯後的指令,即第86行到121行,和foreachTestArray() 方法編譯後的指令,即第137行到172行,兩個方法編譯後的指令是一樣的。
superForeachTestIterator()方法編譯後的指令,即第191行到222行,和foreachTestIterator() 方法編譯後的指令,即第237行到268行,兩個方法編譯後的指令是一樣的。
這說明什麼問題?說明了增強for循環是一個編譯前的概念,在編譯後編譯器會對代碼進行優化。有兩種對象支持增強for循環:一種是數組,編譯器對數組的優化只是寫法上的優化,即superForeachTestArray()方法內的增強for循環在編譯後會變成foreachTestArray() 方法裡for循環的代碼。一種是實現了Iterable接口的對象,對於這種,其實質是拿到對象的迭代器進行遍歷,即superForeachTestIterator()方法內的增強for循環在編譯後會變成foreachTestIterator() 方法內的迭代器遍歷。迭代器遍歷的好處是安全,且它是對遍歷的一種抽象,有種解耦的意味在裡面。