博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从字节码看Java中for-each循环(增强for循环)实现原理
阅读量:4297 次
发布时间:2019-05-27

本文共 9037 字,大约阅读时间需要 30 分钟。

for-each循环是jdk1.5引入的新的语法功能。并不是所有东西都可以使用这个循环的。可以看下Iterable接口的注释,它说明了除了数组外,其他类想要使用for-each循环必须实现这个接口。这一点表明除了数组外的for-each可能底层是由迭代器实现的。

Iterable接口在1.8之前只有一个方法,Iterator<T> iterator(),此方法返回一个迭代器。由于更早出现的Collection接口中早就有了这个同样的方法,所以只需要让Collection接口继承Iterable接口,基于Collection的集合类就可以不做任何更改就使用for-each循环。

对于数组,因为数组不实现Iterable接口,它的for-each实现原理应该和Collection不一样。
下面就通过分析下不同方式编译后的字节码,简单研究下for-each的的底层原理。
一、数组的for-each
下面是的两个很简单的类,可以看出它们的功能是一样的。Java环境使用的是jdk1.8_111。
package iter;public class TestArray {    public static void main(String[] args) {        //String[] a = {"a", "b", "c"};        long[] a = {2L, 3L, 5L};        for (long i : a) {            System.err.println(i);        }    }}
package iter;public class TestArrayFor {    public static void main(String[] args) {        //String[] a = {"a", "b", "c"};        long[] a = {2L, 3L, 5L};        for (int i = 0, len = a.length; i < len; i++) {            System.err.println(a[i]);        }    }}
TestArray使用for-each,TestArrayFor使用传统for循环,使用long数组是为了字节码中好区分int/long。
用javap -c看下两个类的字节码操作,保存成了文本,具体情况如下。
Compiled from "TestArray.java"public class iter.TestArray {  public iter.TestArray();    Code:       0: aload_0       1: invokespecial #8                  // Method java/lang/Object."
":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_3 1: newarray long 3: dup 4: iconst_0 5: ldc2_w #16 // long 2l 8: lastore 9: dup 10: iconst_1 11: ldc2_w #18 // long 3l 14: lastore 15: dup 16: iconst_2 17: ldc2_w #20 // long 5l 20: lastore 21: astore_1 /* 0-21行,创建long数组a,并保存在线程的当前栈帧的局部变量表的第1格*/ 22: aload_1 /* 读取保存在线程的当前栈帧的局部变量表的第1格的对象的引用,就是读取数组a */ 23: dup /* 把a的引用复制一遍并且再放到栈顶 */ 24: astore 6 /* 把栈顶的数存在线程的当前栈帧的局部变量表的第6格,就是生成a的一个值复制品b并存储起来,暂时不知道为什么这里要复制一次,后面的数组都还是用a表示 */ 26: arraylength /* 获取数组长度a.length */ 27: istore 5 /* 把数组长度存储在线程的当前栈帧的局部变量表的第5格,22-27隐式执行了int len = a.length */ 29: iconst_0 /* 读取数字0(这个就是普通的0)到栈中 */ 30: istore 4 /* 把数字0放在线程的当前栈帧的局部变量表的第4格,29-30隐式执行了int i = 0 */ 32: goto 51 /* 无条件跳转到51那个地方,开始循环的代码 */ 35: aload 6 /* 读取数组a */ 37: iload 4 /* 读取i */ 39: laload /* 读取a[i] */ 40: lstore_2 /* 把a[i]存在线程的当前栈帧的局部变量表的第2格 */ 41: getstatic #22 // Field java/lang/System.err:Ljava/io/PrintStream; /* 获取类的static属性,就是System.err */ 44: lload_2 /* 读取存在线程的当前栈帧的局部变量表的第2格的数据,就是读取a[i] */ 45: invokevirtual #28 // Method java/io/PrintStream.println:(J)V /* 执行虚拟机方法,30-36就是执行System.err.println(a[i]) */ 48: iinc 4, 1 /* 将第4格的数字加1,就是执行i++ */ 51: iload 4 /* 读取i */ 53: iload 5 /* 读取a.length */ 55: if_icmplt 35 /* 如果i < len,跳到标记35的那个地方,不满足就往下 */ 58: return}
Compiled from "TestArrayFor.java"public class iter.TestArrayFor {  public iter.TestArrayFor();    Code:       0: aload_0       1: invokespecial #8                  // Method java/lang/Object."
":()V 4: return public static void main(java.lang.String[]); Code: 0: iconst_3 1: newarray long 3: dup 4: iconst_0 5: ldc2_w #16 // long 2l 8: lastore 9: dup 10: iconst_1 11: ldc2_w #18 // long 3l 14: lastore 15: dup 16: iconst_2 17: ldc2_w #20 // long 5l 20: lastore 21: astore_1 /* 0-21行,创建long数组a,并保存在线程的当前栈帧的局部变量表的第1格*/ 22: iconst_0 /* 读取数字0(这个就是普通的0)到栈中 */ 23: istore_2 /* 将栈顶的数字0保存在第二个,22-23就是执行int i = 0; */ 24: aload_1 /* 读取保存在线程的当前栈帧的局部变量表的第1格的对象的引用,就是读取数组a */ 25: arraylength /* 获取数组长度a.length */ 26: istore_3 /* 把数组长度保存在线程的当前栈帧的局部变量表的第3格,24-26就是执行int len = a.length */ 27: goto 42 /* 无条件跳到标记42的那个地方,开始循环的代码 */ 30: getstatic #22 // Field java/lang/System.err:Ljava/io/PrintStream; /* 获取类的static属性,就是System.err */ 33: aload_1 /* 读取数组a */ 34: iload_2 /* 读取i */ 35: laload /* 读取a[i] */ 36: invokevirtual #28 // Method java/io/PrintStream.println:(J)V /* 执行虚拟机方法,30-36就是执行System.err.println(a[i]) */ 39: iinc 2, 1 /* 将第2格的数字加1,就是执行i++ */ 42: iload_2 /* 读取i */ 43: iload_3 /* 读取len */ 44: if_icmplt 30 /* 如果i < len,跳到标记30的那个地方,不满足就往下 */ 47: return}
本人对照下字节码指令表,简单翻译了以下,都写在上面,还算是比较清楚。/**/中的就是本人的注释,//开头的是字节码自带的信息,这些信息不能完全算是注释吧,可以算是对字节码中出现的常量的一种直白翻译,让你看得懂这些常量代表什么。
通过编译后的字节码可以看出,数组的for-each和普通的for循环底层原理是一样的,都是用的普通for循环的那一套。数组的for-each比普通for循环多一点点操作,理论上是要慢一点点,这个暂时也不知道是为什么。这也是语法糖的一些代价,语法越简单,反而越不好进行底层优化。不过这个慢一点那真是一点,在循环体比较复杂时,这个差距就更小了,所以基本上可以认为这两种方式效率一样。实际中根据自己的情况选择,如果需要显式使用下标,就用传统for循环,其他的都可以使用for-each循环。
二、Collection的for-each
还是先贴两段简单的对比的代码,代码逻辑一样。Java环境使用的是jdk1.8_111。
package iter;import java.util.ArrayList;import java.util.List;public class TestFor {    public static void main(String[] args) {        List
listA = new ArrayList
(); for(String str : listA) { System.err.println(str); } }}
package iter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class TestIter {    public static void main(String[] args) {        List
listA = new ArrayList
(); for (Iterator
iter = listA.iterator(); iter.hasNext();) { String s = iter.next(); System.err.println(s); } }}
TestFor是for-each循环,TestIter是使用迭代器循环。
还是跟数组的一样分析,贴下编译后的字节码。
Compiled from "TestFor.java"public class iter.TestFor {  public iter.TestFor();    Code:       0: aload_0       1: invokespecial #8                  // Method java/lang/Object."
":()V 4: return public static void main(java.lang.String[]); Code: 0: new #16 // class java/util/ArrayList 3: dup 4: invokespecial #18 // Method java/util/ArrayList."
":()V 7: astore_1 8: aload_1 9: invokeinterface #19, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 14: astore_3 15: goto 35 18: aload_3 19: invokeinterface #25, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 24: checkcast #31 // class java/lang/String 27: astore_2 28: getstatic #33 // Field java/lang/System.err:Ljava/io/PrintStream; 31: aload_2 32: invokevirtual #39 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 35: aload_3 36: invokeinterface #45, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 41: ifne 18 44: return}
Compiled from "TestIter.java"public class iter.TestIter {  public iter.TestIter();    Code:       0: aload_0       1: invokespecial #8                  // Method java/lang/Object."
":()V 4: return public static void main(java.lang.String[]); Code: 0: new #16 // class java/util/ArrayList 3: dup 4: invokespecial #18 // Method java/util/ArrayList."
":()V 7: astore_1 8: aload_1 9: invokeinterface #19, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 14: astore_2 15: goto 35 18: aload_2 19: invokeinterface #25, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 24: checkcast #31 // class java/lang/String 27: astore_3 28: getstatic #33 // Field java/lang/System.err:Ljava/io/PrintStream; 31: aload_3 32: invokevirtual #39 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 35: aload_2 36: invokeinterface #45, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 41: ifne 18 44: return}
这两段字节码中自带的注释很多,基本上看得懂,就不添加注释了。
两段字节码除了几个变量保存在线程的当前栈帧的局部变量表的索引(astore_n,这个n就是索引)不一样外,其余的都是一模一样的。不排除某次编译后连那几个索引值也一样,那就真一模一样了。字节码自带的注释都说了,Collection的for-each底层也是使用迭代器来实现的,两种方式可以说是完全等价的。
对于实现了RandomAccess接口的实现类,因为它们的随机访问操作的时间复杂度为O(1),大多数情况使用传统for循环会比用迭代器循环(这里的迭代器也可以用for-each替换,上面说了它们底层整体是一样的)要快。至于这一点是为什么,可以看下ArrayList的源码。它的迭代器虽然也是通过下标直接访问elementData数组,但是迭代器多了很多方法调用以及其他的额外操作,现在很多编译器cpu也都会对传统for循环进行特别的优化,在这个层面十几个指令的差别就很大了,这些因素加在一起导致RandomAccess的迭代器比传统for循环要慢一些。对于ArrayList这种,在cpu密集型的应用中应该只使用传统for循环,在循环体执行时间比较长的应用中,传统for循环和迭代器循环的差别就很小了,这时候使用迭代器(for-each循环)也不会明显降低执行效率。
参考:
1、https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
2、Java虚拟机规范(Java SE 8)
以上内容如有问题,烦请指出,谢谢!

转载地址:http://qrcws.baihongyu.com/

你可能感兴趣的文章
8、JavaWEB学习之基础篇—文件上传&下载
查看>>
reRender属性的使用
查看>>
href="javascript:void(0)"
查看>>
h:panelGrid、h:panelGroup标签学习
查看>>
f:facet标签 的用法
查看>>
<h:panelgroup>相当于span元素
查看>>
java中append()的方法
查看>>
必学高级SQL语句
查看>>
经典SQL语句大全
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>
log日志记录是什么
查看>>
<rich:modelPanel>标签的使用
查看>>
<h:commandLink>和<h:inputLink>的区别
查看>>
<a4j:keeyAlive>的英文介绍
查看>>
关于list对象的转化问题
查看>>
VOPO对象介绍
查看>>
suse创建的虚拟机,修改ip地址
查看>>
linux的挂载的问题,重启后就挂载就没有了
查看>>
docker原始镜像启动容器并创建Apache服务器实现反向代理
查看>>
docker容器秒死的解决办法
查看>>