Java: for(;;) vs. while(true)
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:
And the bytecode for while(true){}
looks like this:
So yes, at least for me, they're identical.
所以,代码中使用while(true)还是for(;;),只是个人喜好问题,编译后的字节码是相同的,无所谓哪个性能更好。
验证
Java代码