标准while(true)
循环和for(;;)
之间的区别是什么
有没有,或者在编译后两者都会映射到同一字节码?
发布于 2012-01-16 22:00:39
你自己决定用哪一个。因为它们等同于编译器。
创建文件:
// first test
public class Test {
public static void main(String[] args) {
while (true) {
System.out.println("Hi");
}
}
}
编译:
javac -g:none Test.java
rename Test.class Test1.class
创建文件:
// second test
public class Test {
public static void main(String[] args) {
for (;;) {
System.out.println("Hi");
}
}
}
编译:
javac -g:none Test.java
mv Test.class Test2.class
比较:
diff -s Test1.class Test2.class
Files Test1.class and Test2.class are identical
发布于 2012-01-16 21:51:29
在Oracle Java 7上,您会得到相同的字节代码。你不能从原始文件中使用的字节代码中辨别出来。哪一个是最好的是品味问题。我使用while(true)
发布于 2012-01-16 21:46:01
根据编译器的不同,它应该映射到相同的字节代码。
https://stackoverflow.com/questions/8880870
复制相似问题