Java syntax sugar 2:foreach cycle

Source: Internet
Author: User

Enhanced for loop features stronger and cleaner code than normal for loop

Write a section of code:

    @Test    publicvoid  Test_foreach () {        Listnew arraylist< String>();        List.add ("1111");        List.add ("2222");          for (String str:list) {            System.out.println (str);        }    }

In fact, I used jdk6, directly in the class file can be seen, foreach has been transformed into an iterator iterator implementation, jdk8 seemingly not, can only look at the compiled bytecode file, javap-c Test.class:

   Public voidTest_foreach (); Code:0:New#46//class Java/util/arraylist3: DUP4:invokespecial #47//Method java/util/arraylist. " <init> ":() V 7: Astore_18: Aload_19:LDC #48//String 111111:invokeinterface #49, 2//Interfacemethod java/util/list.adD: (ljava/lang/Object;) Z16: Pop17: Aload_118:LDC #50//String 222220:invokeinterface #49, 2//Interfacemethod java/util/list.adD: (ljava/lang/Object;) Z25: Pop26: Aload_127:invokeinterface #51, 1//Interfacemethod java/util/list.itErator: () ljava/util/Iterator; 32: astore_233: Aload_234:invokeinterface #52, 1//Interfacemethod Java/util/iteratoR.hasnext: () Z39:ifeq 62 42: Aload_243:invokeinterface #53, 1//Interfacemethod Java/util/iteratoR.next: () ljava/lang/Object; 48:checkcast #40//class Java/lang/string51: Astore_352:getstatic #12//Field java/lang/system.out:ljava/io/PrintStream; 55: Aload_356:invokevirtual #14//Method java/io/printstream.printlN: (ljava/lang/String;) V59:Goto33 62:return

New, DUP, invokespecial the instructions defined in the bytecode instruction table to execute the specified C + + code, we do not understand that it is OK, the first temporarily not concerned. But we can see List.iterator (), IteratoR.hasnext () This information, which means that the foreach bottom is implemented by iterators, the condition of implementing iterators is to implement the iterator interface IteratoR, It is obvious that the list inherits the collection interface, and the collection interface inherits the IteratoR interface.

But arrays can also use a foreach loop, but it doesn't inherit the iterator Iterator interface, what's going on?

    @Test    publicvoid  Test_foreach_arr () {        = {"111", "222"};          for (String str:ss) {            System.out.println (str);        }    }

Look at the bytecode file:

   Public voidTest_foreach_arr (); Code:0: Iconst_21:anewarray #40//class Java/lang/string4: DUP5: Iconst_06:LDC #41//String 1118: Aastore9: DUP10: Iconst_111:LDC #42//String 22213: Aastore14: Astore_115: Aload_116: astore_217: Aload_218: Arraylength19: Istore_320: Iconst_021:istore 4 23:iload 4 25: Iload_326:IF_ICMPGE 49 29: Aload_230:iload 4 32: Aaload33:astore 5 35:getstatic #12//Field java/lang/system.out:ljava/io/PrintStream; 38:aload 5 40:invokevirtual #14//Method java/io/printstream.printlN: (ljava/lang/String;) V43:iinc 4, 1 46:Goto23 49:return

In fact, this bytecode I really do not understand, online someone said to look at the goto statement, it seems that the array of foreach loop is actually used or for the loop, then we write a for loop than the next two bytes code.

Array for loop implementation:

    @Test    publicvoid  test_for () {        = {"111", "222"};          for (int i = 0; i < ss.length; i++) {            System.out.println (ss[i]);        }    }

Byte-code files:

   Public voidtest_for (); Code:0: Iconst_21:anewarray #40//class Java/lang/string4: DUP5: Iconst_06:LDC #41//String 1118: Aastore9: DUP10: Iconst_111:LDC #42//String 22213: Aastore14: Astore_115: Iconst_016: istore_217: Iload_218: Aload_119: Arraylength20:if_icmpge 23:getstatic #12//Field java/lang/system.out:ljava/io/PrintStream; 26: Aload_127: Iload_228: Aaload29:invokevirtual #14//Method java/io/printstream.printlN: (ljava/lang/String;) V32:iinc 2, 1 35:Goto17 38:return

Compared to the discovery, in fact, the array of foreach Loop and for loop run is consistent, and then look back to the bytecode there is a arraylength, basically can be linked to think, the final array of foreach is also through the length of the array to traverse.

Java syntax sugar 2:foreach cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.