Java:for(;;)vs. while(true) [英] Java: for(;;) vs. while(true)

查看:106
本文介绍了Java:for(;;)vs. while(true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准 while(true)循环和 for(;;)?之间有什么区别? p>

有没有,或者两者都会在编译后映射到相同的字节码?

解决方案 while(true)看起来更干净,乍一看更容易阅读和理解。在Java中,它们都不会导致编译器警告。

在字节码级别上,它可能取决于编译器和优化级别,但原则上所发射的代码应该是同样的。

编辑:

在我的编译器中,使用字节码大纲插件, for(;;)的字节码{}

  L0 
LINENUMBER 6 L0
相框
GOTO L0

字节码为 while(true){}

  L0 
LINENUMBER 6 L0
相框
GOTO L0

所以是的,至少对我来说,它们是相同的。

What is the difference between a standard while(true) loop and for(;;)?

Is there any, or will both be mapped to the same bytecode after compiling?

解决方案

Semantically, they're completely equivalent. It's a matter of taste, but I think while(true) looks cleaner, and is easier to read and understand at first glance. In Java neither of them causes compiler warnings.

At the bytecode level, it might depend on the compiler and the level of optimizations, but in principle the code emitted should be the same.

EDIT:

On my compiler, using the Bytecode Outline plugin,the bytecode for for(;;){} looks like this:

   L0
    LINENUMBER 6 L0
   FRAME SAME
    GOTO L0

And the bytecode for while(true){} looks like this:

   L0
    LINENUMBER 6 L0
   FRAME SAME
    GOTO L0

So yes, at least for me, they're identical.

这篇关于Java:for(;;)vs. while(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆