for (; ; )和while (true) 没有区别

看jdk源码的时候,很多地方都是用的for (; ; ),难道跟while (true)有何区别,用javap反汇编了下,实际是一样的

javap  -v  target.test-classes.com.example.demoweb.DemoWebApplicationTests 

  源码

public class DemoWebApplicationTests {


    public void test1() {
        for (; ; ) {
            System.out.println(11);
        }
    }

    public void test2() {
        while (true) {
            System.out.println(11);
        }
    }
}  

  反汇编后代码

Warning: Binary file target.test-classes.com.example.demoweb.DemoWebApplicationTests contains com.example.demoweb.DemoWebApplicationTests
Classfile /Users/pigercc/IdeaProjects/demo-web/target/test-classes/com/example/demoweb/DemoWebApplicationTests.class
  Last modified Mar 15, 2019; size 632 bytes
  MD5 checksum 7c14c6fc2447db0f652238e10b70d796
  Compiled from "DemoWebApplicationTests.java"
public class com.example.demoweb.DemoWebApplicationTests
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#18         // java/lang/Object."<init>":()V
   #2 = Fieldref           #19.#20        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #21.#22        // java/io/PrintStream.println:(I)V
   #4 = Class              #23            // com/example/demoweb/DemoWebApplicationTests
   #5 = Class              #24            // java/lang/Object
   #6 = Utf8               <init>
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               LocalVariableTable
  #11 = Utf8               this
  #12 = Utf8               Lcom/example/demoweb/DemoWebApplicationTests;
  #13 = Utf8               test1
  #14 = Utf8               StackMapTable
  #15 = Utf8               test2
  #16 = Utf8               SourceFile
  #17 = Utf8               DemoWebApplicationTests.java
  #18 = NameAndType        #6:#7          // "<init>":()V
  #19 = Class              #25            // java/lang/System
  #20 = NameAndType        #26:#27        // out:Ljava/io/PrintStream;
  #21 = Class              #28            // java/io/PrintStream
  #22 = NameAndType        #29:#30        // println:(I)V
  #23 = Utf8               com/example/demoweb/DemoWebApplicationTests
  #24 = Utf8               java/lang/Object
  #25 = Utf8               java/lang/System
  #26 = Utf8               out
  #27 = Utf8               Ljava/io/PrintStream;
  #28 = Utf8               java/io/PrintStream
  #29 = Utf8               println
  #30 = Utf8               (I)V
{
  public com.example.demoweb.DemoWebApplicationTests();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 11: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/example/demoweb/DemoWebApplicationTests;

  public void test1();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: bipush        11
         5: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
         8: goto          0
      LineNumberTable:
        line 16: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      11     0  this   Lcom/example/demoweb/DemoWebApplicationTests;
      StackMapTable: number_of_entries = 1
        frame_type = 0 /* same */

  public void test2();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: bipush        11
         5: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
         8: goto          0
      LineNumberTable:
        line 22: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      11     0  this   Lcom/example/demoweb/DemoWebApplicationTests;
      StackMapTable: number_of_entries = 1
        frame_type = 0 /* same */
}
SourceFile: "DemoWebApplicationTests.java"

  

posted @ 2019-03-15 14:28  pigercc  阅读(382)  评论(0编辑  收藏  举报