Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker DevOps


Java 语法糖 - 增强 for 循环

Java 大约 4190 字

本质

对于数组而言,会转换为普通for循环。

对于集合而言,会转换为iterator迭代器。

数组

对于数组而言,增强for循环本质是for(int i = 0; i < arr.length(); ++i)

Java 代码

public class Test4 {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        for (int i : arr) {

        }
    }

}

反编译 class

可以看到:

编译成字节码后,转换成了普通for循环。(可以使用反编译工具,或者直接在IDEA中打开.class文件)

public class Test4 {
    public Test4() {
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3};
        int[] var2 = arr;
        int var3 = arr.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            int var10000 = var2[var4];
        }

    }
}

字节码

public static void main(java.lang.String[]);
  descriptor: ([Ljava/lang/String;)V
  flags: (0x0009) ACC_PUBLIC, ACC_STATIC
  Code:
    stack=4, locals=6, args_size=1
       0: iconst_3
       1: newarray       int
       3: dup
       4: iconst_0
       5: iconst_1
       6: iastore
       7: dup
       8: iconst_1
       9: iconst_2
      10: iastore
      11: dup
      12: iconst_2
      13: iconst_3
      14: iastore
      15: astore_1
      16: aload_1
      17: astore_2
      18: aload_2
      19: arraylength
      20: istore_3
      21: iconst_0
      22: istore        4
      24: iload         4
      26: iload_3
      27: if_icmpge     42
      30: aload_2
      31: iload         4
      33: iaload
      34: istore        5
      36: iinc          4, 1
      39: goto          24
      42: return
    LineNumberTable:
      line 6: 0
      line 7: 16
      line 10: 42
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      43     0  args   [Ljava/lang/String;
         16      27     1   arr   [I
    StackMapTable: number_of_entries = 2
      frame_type = 255 /* full_frame */
        offset_delta = 24
        locals = [ class "[Ljava/lang/String;", class "[I", class "[I", int, int ]
        stack = []
      frame_type = 248 /* chop */
        offset_delta = 17
  MethodParameters:
    Name                           Flags
    args

集合

Java 代码

public class Test5 {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer integer : list) {

        }
    }

}

反编译 class

可以看到:

编译成字节码后,转换成了iterator迭代器。

public class Test5 {
    public Test5() {
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList();

        Integer var3;
        for(Iterator var2 = list.iterator(); var2.hasNext(); var3 = (Integer)var2.next()) {
        }

    }
}

字节码

public static void main(java.lang.String[]);
  descriptor: ([Ljava/lang/String;)V
  flags: (0x0009) ACC_PUBLIC, ACC_STATIC
  Code:
    stack=2, locals=4, args_size=1
       0: new           #2                  // class java/util/ArrayList
       3: dup
       4: invokespecial #3                  // Method java/util/ArrayList."<init>":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #4                  // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
      12: astore_2
      13: aload_2
      14: invokeinterface #5,  1            // InterfaceMethod java/util/Iterator.hasNext:()Z
      19: ifeq          35
      22: aload_2
      23: invokeinterface #6,  1            // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      28: checkcast     #7                  // class java/lang/Integer
      31: astore_3
      32: goto          13
      35: return
    LineNumberTable:
      line 8: 0
      line 9: 8
      line 11: 32
      line 12: 35
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      36     0  args   [Ljava/lang/String;
          8      28     1  list   Ljava/util/ArrayList;
    LocalVariableTypeTable:
      Start  Length  Slot  Name   Signature
          8      28     1  list   Ljava/util/ArrayList<Ljava/lang/Integer;>;
    StackMapTable: number_of_entries = 2
      frame_type = 253 /* append */
        offset_delta = 13
        locals = [ class java/util/ArrayList, class java/util/Iterator ]
      frame_type = 250 /* chop */
        offset_delta = 21
  MethodParameters:
    Name                           Flags
    args
阅读 425 · 发布于 2022-04-28

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批