美文网首页
一、while(true)和for(;;)分析

一、while(true)和for(;;)分析

作者: 此间有道 | 来源:发表于2020-05-19 19:03 被阅读0次

    由字节码看,两者一致。

    public class WhileForTest {
        public void forFunc() {
            for (;;) {
                System.out.println("for loop");
            }
        }
    
        public void whileFunc() {
            while (true) {
                System.out.println("while loop");
            }
        }
    }
    
    public class jmh/WhileForTest {
    
      // compiled from: WhileForTest.java
    
      // access flags 0x1
      public <init>()V
       L0
        LINENUMBER 7 L0
        ALOAD 0
        INVOKESPECIAL java/lang/Object.<init> ()V
        RETURN
       L1
        LOCALVARIABLE this Ljmh/WhileForTest; L0 L1 0
        MAXSTACK = 1
        MAXLOCALS = 1
    
      // access flags 0x1
      public forFunc()V
       L0
        LINENUMBER 10 L0
       FRAME SAME
        GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
        LDC "for loop"
        INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
        GOTO L0
       L1
        LOCALVARIABLE this Ljmh/WhileForTest; L0 L1 0
        MAXSTACK = 2
        MAXLOCALS = 1
    
      // access flags 0x1
      public whileFunc()V
       L0
        LINENUMBER 16 L0
       FRAME SAME
        GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
        LDC "while loop"
        INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
        GOTO L0
       L1
        LOCALVARIABLE this Ljmh/WhileForTest; L0 L1 0
        MAXSTACK = 2
        MAXLOCALS = 1
    }
    

    相关文章

      网友评论

          本文标题:一、while(true)和for(;;)分析

          本文链接:https://www.haomeiwen.com/subject/gyqiohtx.html