Java Reference
In-Depth Information
if(x<0||y>0){
x=y;
}
to produce
58:iload_1
59:iflt66
62:iload_2
63:ifle68
66:iload_2
67:istore_1
68:...
Exercise 5.13. Write tests that involve conditional branches on nested Boolean expressions
that involve the, ! , && and (if you have implemented it) || operator to insure that the
compiler is not generating branches to branches.
Exercise 5.14. Add the switch-statement to j--, adding it to your compiler and testing it
thoroughly.
The switch-statement is more involved than other control constructs. Fortunately, the
JVM provides special support for it. Code generation for the switch-statement is discussed
in Section 6.10 of the JVM specification [Lindholm and Yellin, 1999].
As an example, let us consider a method that makes use of a simple switch-statement.
intdigitTight(charc){
switch(c){
case'1':return1;
case'0':return0;
case'2':return2;
default:return-1;
}
}
This method converts digit characters '0' , '1' , and '2' to the integers they denote; any
other character is converted to 1. This is not the best way to do this conversion but it
makes for a simple example of the switch-statement. Let us take a look at the code that
javac produces, using the javap tool:
intdigitTight(char);
Code:
Stack=1,Locals=2,Args_size=2
0:iload_1
1:tableswitch{//48to50
48:30;
49:28;
50:32;
default:34}
28:iconst_1
29:ireturn
30:iconst_0
31:ireturn
32:iconst_2
33:ireturn
34:iconst_m1
35:ireturn
Notice a few things here. First, the JVM uses a table to map the Unicode character repre-
sentations to the code locations for each of the corresponding cases. For example, 48 (the
Unicode representation for the character '0' ) maps to location 30, the location of the JVM
 
Search WWH ::




Custom Search