(一)Java中  for(;;)和while(true)都是死循环。

1.0 代码: for

  1. @Test  
  2. public void test(){  
  3.     for(;;){  
  4.         System.out.println("for"+"+++");  
  5.     }   
  6. }

编译后的字节码:

  1. public void test();  
  2.    flags: ACC_PUBLIC  
  3.    Code:  
  4.      stack=2, locals=1, args_size=1  
  5.         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;  
  6.         3: ldc           #3                  // String for+++  
  7.         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V  
  8.         8goto          0  
  9.      LineNumberTable:  
  10.        line 180  
  11.      LocalVariableTable:  
  12.        Start  Length  Slot  Name   Signature  
  13.               0      11     0  this   Lcom/jj/jingcai/MyTest;  
  14.      StackMapTable: number_of_entries = 1  
  15.           frame_type = 0 /* same */  
  16.    RuntimeVisibleAnnotations:  
  17.      0: #17()  

2.0 while(true)

  1. @Test  
  2. public void test(){  
  3.     while (true){  
  4.         System.out.println("while"+"---");  
  5.     }  
  6.   

编译后的字节码:

  1. public void test();  
  2.   flags: ACC_PUBLIC  
  3.   Code:  
  4.     stack=2, locals=1, args_size=1  
  5.        0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;  
  6.        3: ldc           #3                  // String while---  
  7.        5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V  
  8.        8goto          0  
  9.     LineNumberTable:  
  10.       line 180  
  11.     LocalVariableTable:  
  12.       Start  Length  Slot  Name   Signature  
  13.              0      11     0  this   Lcom/jj/jingcai/MyTest;  
  14.     StackMapTable: number_of_entries = 1  
  15.          frame_type = 0 /* same */  
  16.   RuntimeVisibleAnnotations:  
  17.     0: #17() 


总结:两者都是用于 无限循环的判断语句。且编译后的字节码  完全相同

但是:注意  这是 依赖于编译器优化后的结果。--->有些不优化的编译器-->则会体现出不同。for(;;)的指令就会少一些。而while(true)会用到寄存器,就会多一些指令。

eg:for(;;)和while(true)的区别

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
  • 2021-10-18
  • 2022-12-23
  • 2021-08-17
  • 2021-07-12
猜你喜欢
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
相关资源
相似解决方案