Java:for(;;)vs. while(true) [英] Java: for(;;) vs. while(true)
问题描述
标准 while(true)
循环和 for(;;)
?之间有什么区别? p>
有没有,或者两者都会在编译后映射到相同的字节码?
在字节码级别上,它可能取决于编译器和优化级别,但原则上所发射的代码应该是同样的。
编辑: 在我的编译器中,使用字节码大纲插件, 字节码为 所以是的,至少对我来说,它们是相同的。 What is the difference between a standard 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 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 And the bytecode for So yes, at least for me, they're identical. 这篇关于Java:for(;;)vs. while(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
for(;;)的字节码{} $ c $
L0
LINENUMBER 6 L0
相框
GOTO L0
while(true){} $ c $
L0
LINENUMBER 6 L0
相框
GOTO L0
while(true)
loop and for(;;)
?while(true)
looks cleaner, and is easier to read and understand at first glance. In Java neither of them causes compiler warnings.for(;;){}
looks like this: L0
LINENUMBER 6 L0
FRAME SAME
GOTO L0
while(true){}
looks like this: L0
LINENUMBER 6 L0
FRAME SAME
GOTO L0