Java中为什么数组没有实现Iterable接口,但可以使用foreach语句遍历?

本质是转成fori形式的循环,上界是数组的size

直接上代码

package com.finchina;

public class TstMain1 {

    public static void main(String[] args) {
        int[] ints = new int[5];
        for (int i = 0; i < 5; i++) {
            ints[i] = i * i;
        }
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }
}

 

反编译后的

package com.finchina;

public class TstMain1 {
    public TstMain1() {
    }

    public static void main(String[] var0) {
        int[] var1 = new int[5];

        for(int var2 = 0; var2 < 5; ++var2) {
            var1[var2] = var2 * var2;
        }

        int[] var6 = var1;
        int var3 = var1.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            int var5 = var6[var4];
            System.out.println(var5);
        }

    }
}

javap -v 之后的:

Classfile /F:/worklearn/java_learn/src/main/java/com/finchina/TstMain1.class
  Last modified 2019-9-6; size 526 bytes
  MD5 checksum efa3f98ca54973703f187cf3d4ae0b57
  Compiled from "TstMain1.java"
public class com.finchina.TstMain1
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#16         // java/lang/Object."":()V
   #2 = Fieldref           #17.#18        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #19.#20        // java/io/PrintStream.println:(I)V
   #4 = Class              #21            // com/finchina/TstMain1
   #5 = Class              #22            // java/lang/Object
   #6 = Utf8               
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               main
  #11 = Utf8               ([Ljava/lang/String;)V
  #12 = Utf8               StackMapTable
  #13 = Class              #23            // "[I"
  #14 = Utf8               SourceFile
  #15 = Utf8               TstMain1.java
  #16 = NameAndType        #6:#7          // "":()V
  #17 = Class              #24            // java/lang/System
  #18 = NameAndType        #25:#26        // out:Ljava/io/PrintStream;
  #19 = Class              #27            // java/io/PrintStream
  #20 = NameAndType        #28:#29        // println:(I)V
  #21 = Utf8               com/finchina/TstMain1
  #22 = Utf8               java/lang/Object
  #23 = Utf8               [I
  #24 = Utf8               java/lang/System
  #25 = Utf8               out
  #26 = Utf8               Ljava/io/PrintStream;
  #27 = Utf8               java/io/PrintStream
  #28 = Utf8               println
  #29 = Utf8               (I)V
{
  public com.finchina.TstMain1();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return
      LineNumberTable:
        line 3: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=6, args_size=1
         0: iconst_5
         1: newarray       int
         3: astore_1
         4: iconst_0
         5: istore_2
         6: iload_2
         7: iconst_5
         8: if_icmpge     23
        11: aload_1
        12: iload_2
        13: iload_2
        14: iload_2
        15: imul
        16: iastore
        17: iinc          2, 1
        20: goto          6
        23: aload_1
        24: astore_2
        25: aload_2
        26: arraylength
        27: istore_3
        28: iconst_0
        29: istore        4
        31: iload         4
        33: iload_3
        34: if_icmpge     57
        37: aload_2
        38: iload         4
        40: iaload
        41: istore        5
        43: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        46: iload         5
        48: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
        51: iinc          4, 1
        54: goto          31
        57: return
      LineNumberTable:
        line 6: 0
        line 7: 4
        line 8: 11
        line 7: 17
        line 10: 23
        line 11: 43
        line 10: 51
        line 13: 57
      StackMapTable: number_of_entries = 4
        frame_type = 253 /* append */
          offset_delta = 6
          locals = [ class "[I", int ]
        frame_type = 250 /* chop */
          offset_delta = 16
        frame_type = 254 /* append */
          offset_delta = 7
          locals = [ class "[I", int, int ]
        frame_type = 248 /* chop */
          offset_delta = 25
}
SourceFile: "TstMain1.java"

 

你可能感兴趣的:(Java,java)