To parse a switch statement in Java by byte-code

Source: Internet
Author: User
Tags case statement

In one of the problems encountered in the switch, because of the switch execution sequence of the lack of understanding, here is a simple way to understand the byte code of the Switch execution order (the topic is as follows):

 Public classag{Static  Public inti=10;  Public Static voidMain (String []args) {Switch(i) {default: System.out.println ("This is default");  Case1: System.out.println ("This is one");  Case2: System.out.println ("This is the");  Case10: System.out.println ("This is ten");  Case5: System.out.println ("This is five"); }    }}

The output is:

Here's an example to analyze switch:

First, the code 1 as shown:

Simply say the code, the code is very simple, is to build a main method in a class, and then use a switch statement in the main method (note that this code is the first in the default), and then through the JAVAP tool to get the byte code, In turn, the execution mode of switch is analyzed, and the resulting bytecode is as follows:

Byte code analysis is as follows:

 Public Static voidMain (java.lang.string[]); Code:0:iconst_5//Enter constant 5 into the Operation Stack1:istore_1//storing top-of-stack data in a local variable 12:iload_1//put the first local variable into a stack3:lookupswitch {//2//a switch appears, which is bound to be a switch statement.1:33//when A==1, execute the statement of label 332:38//when a==2, execute the statement of label 38              default: 28//The default case, although the statement is in front, but the judgment is still final judgment, the execution of the statement of the label 28         }     28:iconst_5//start executing content from default29:istore_2//28 and 29 statements are similar to int b =530:Goto43//since the use of break, jump out of the statement, so EXECUTE statement 43, that is, jump out of switch, the same33:iconst_5<span style= "White-space:pre" > </span>//action to start execution of A==134: istore_235:Goto43//If you do not break, you will continue to execute the next statement instead of jumping out of the switch38:iconst_5 <span style= "font-family:arial, Helvetica, Sans-serif;" >//What to do when you start A==1 </span>39: istore_240:Goto43 43:return   //Main method Execution End

Two, the second Test code (default is placed on the last side):

The same simple code, the code is very simple, is to build a main method in a class , and then use a switch statement in the Main method (note that the code is the default in the last side), and then through the The JAVAP tool obtains the bytecode, and then analyzes the mode of switch execution, resulting in the following byte code:

The word decoding analysis is as follows, just focus on the similarities and differences of bytecode in two code:

 Public Static voidMain (java.lang.string[]); Code:0: Iconst_51: Istore_12: Iload_13:lookupswitch {//21:28-2:33default: 38//It is important to note that default is still on the last side, and the only thing that changes is the position of the execution statement          }      28: Iconst_5 The execution position of the//a==1 statement29: istore_230:Goto43 33: iconst_5//a==2 statement Execution Location34: istore_235:Goto43 38: iconst_5//default statement Execution Location39: istore_240:Goto43 43:return}

Summarize:

From the above two code, you can get:

1, regardless of switch in the default location, it is the last to judge;

2, although the default is the last to be judged, but the position of the statement in default is still related to the position of default in the sentence (this sentence is close to the nonsense, but when there is no break in the case statement, the entire switch is similar to the sequential execution)

3. When there is no break in the case, its execution order and execution is continued down until the switch ends or a break is encountered.

Finally, a small example to test the analysis of this article (how so familiar, yes, that is the beginning of the code slightly changed a bit):

 Public classag{Static  Public inti=10;  Public Static voidMain (String []args) {Switch(i) {default: System.out.println ("This is default");  Case1: System.out.println ("This is one");  Case2: System.out.println ("This is the");  Case5: System.out.println ("This is five"); }    }}

The result is needless to say.

To parse a switch statement in Java by byte-code

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.