Parsing Java for loops with bytecode

Source: Internet
Author: User

The Fou loop is often used, but how does it execute in the bytecode layer? Motivated by interest, we have this short article!

The first thing to do is to parse the bytecode and write a class with the following code:

public class Fortest{public static void Main (string[] args) {for (int i = 0; i <; ++i) {System.out.println (i);}}}

A simple for loop prints 10 numbers, so after compiling it's time to get his bytecode, use the command

Javap-v Fortest.class > FORTEST.J

Here with the-v parameter, export the entire class file details, if just want to simply get the bytecode, just replace with the-c parameter, the result is as follows:

classfile /e:/develop/javatest/fortest.class  last modified 2015-1-2; size  437 bytes  md5 checksum 21aad18f3a51ffa226ad2f49d7c3f5e0  compiled  from  "Fortest.java" public class fortest  sourcefile:  "ForTest.java"    Minor version: 0  major version: 51  flags: acc_public, acc_ superconstant pool:    #1  = Methodref            #5. #15          //  java/lang/object. " <init> ":() v    #2  = Fieldref             #16. #17         //  java/lang/system.out: ljava/io/printstream;    #3  = Methodref            #18. #19 &NBsp;       //  java/io/printstream.println: (I) V    #4  = Class               #20             //  ForTest     #5  = Class               #21             //  java/lang/object     #6  = Utf8                <init>    #7  = Utf8                 () v    #8  = Utf8                Code    #9  =  Utf8               linenumbertable   #10  =  utf8               main   #11  = Utf8                ([ljava/lang/string;) v   #12  = Utf8                StackMapTable   #13  = Utf8                SourceFile   #14  = utf8                ForTest.java    #15  = NameAndType         #6: #7            //   "<init>":() v   #16  = Class                #22              //  java/lang/System   #17  = NameAndType          #23: #24         //  out:ljava/ io/printstream;   #18  = Class                #25             //   java/io/PrintStream   #19  = NameAndType          #26: #27         //  println: (I) v   #20  =  utf8               fortest    #21  = Utf8                java/lang/object   #22  = Utf8                java/lang/System   #23  = Utf8                out   #24  = Utf8                Ljava/io/PrintStream;   #25  =  utf8               java/io/ printstream   #26  = Utf8                println   #27  = Utf8                 (I) v{  public fortest ();     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 1: 0  public static void main (java.lang.String[]);     flags: ACC_PUBLIC, ACC_STATIC    Code:       stack=2, locals=2, args_size=1          0: iconst_0                1: istore_1               2: iload_1                 3: bipush         10         5: if_icmpge      21         8: getstatic       #2                    // Field java/lang/System.out:Ljava/io/PrintStream;         11: iload_1                12: invokevirtual  #3                    // method java/io/printstream.println: (I) V         15: iinc          1, 1         18: goto           2        21: return               LineNumberTable:         line 3: 0        line 4: 8         line 3: 15        line  6: 21      StackMapTable: number_of_entries = 2            frame_type = 252 /* append  */             offset_delta =  2        locals = [ int ]            frame_type = 250 /* chop */        &NBSP;&NBSP;&NBSP;OFFSET_DELTA&NBSP;=&NBSP;18}

In fact, the focus is still the following code, the other content is just easy to understand and analyze the class file:

         0: iconst_0                1: istore_1                2: iload_1                 3: bipush         10         5: if_icmpge      21         8: getstatic     # 2                  / / field java/lang/system.out:ljava/io/printstream;        11 :  iload_1               12:  invokevirtual  #3                  //  method java/io/printstream.println: (I) v        15:  iinc          1, 1         18: goto          2         21: return

Now analyze how the for loop is executed.

First Initialize the loop variable first:

0:iconst_0 1:istore_1

These two lines of code are equivalent to the code int i = 0 (Iconst_0 is the number 0,istore_1 is the local variable 1, here is the source of i)

then judge the loop condition:

2:iload_1 3:bipush 5:if_icmpge 21

These three lines mean I is less than 10, less than continue to execute, or jump to the return of the number 21 is also jumping out for the For loop (Iload_1 refers to read the local variable 1 that is the source of i)

If the condition is established, then the for loop body is executed:

8:getstatic #2//Field Java/lang/system.out:ljava/io/printstream; 11:iload_1 12:invokevirtual #3//Method java/io/printstream.println: (I) V

These three rows result in the execution of System.out.println (i); This line of code

The meaning of the #2 and #3 represented here can be found in the constant pool, so I used the-v parameter to make it easier to understand when using the JAVAP command.

Now that the loop body is done , the loop variable is updated:

15:iinc 1, 1

namely ++i; Statement

The last thing to do is jump to the judgment statement:

18:goto 2



Simple analysis of A for loop that's it, it's wrong or wrong.

Parsing Java for loops with bytecode

Related Article

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.