zoukankan      html  css  js  c++  java
  • 问:java中for (;;) 与while (true)运行有什么区别呢?哪个效率更高?

    比较一下JVM字节码:

    while(true):

    public class WhileAndFor {
    public void test(){
    while(true){
    System.out.println("while...");
    }
    }
    }
    0 getstatic #2 <java/lang/System.out>
    3 ldc #3 <while...>
    5 invokevirtual #4 <java/io/PrintStream.println>
    8 goto 0 (-8)

    for(;;):

    public class WhileAndFor {
        public void test(){
            for(;;){
                System.out.println("while...");
            }
        }
    }
    0 getstatic #2 <java/lang/System.out>
    3 ldc #3 <while...>
    5 invokevirtual #4 <java/io/PrintStream.println>
    8 goto 0 (-8)

    根据while与for;;的字节码看,一模一样,没有区别。都是执行完一句后再“GOTO”到最开始。

    所以结论就是:java中 while(true)和for(;;)效率一样,没有区别

  • 相关阅读:
    java final计算
    浅析Java中的final关键字
    easyui
    Java:类与继承
    java中&和&&
    XML
    JSON
    SQL
    selenium
    Metasploit
  • 原文地址:https://www.cnblogs.com/yibao/p/14446762.html
Copyright © 2011-2022 走看看