📄 chap16.html
字号:
<P> }</P>
<P> }</P>
<P> }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The bytecodes generated by <CODE>javac</CODE> for the <CODE>argue()</CODE> method are shown below: </P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 iconst_0 // Push constant 0 (TOMAYTO)
<P> 1 istore_0 // Pop into local var 0: int say = TOMAYTO;</P>
<P> 2 iload_0 // Push key for switch from local var 0</P>
<P> // Perform switch statement: switch (say) {...</P>
<P> // Low case value is 0, high case value is 1</P>
<P> // Default branch offset will goto 2</P>
<P> 3 tableswitch 0 to 1: default=2</P>
<P> 0: 24 // case 0 (TOMAYTO): goto 24</P>
<P> 1: 29 // case 1 (TOMAHTO): goto 29</P>
<P> // Note that the next instruction starts at address</P>
<P> // 24, which means the tableswitch took up 21 bytes</P>
<P>24 iconst_1 // Push constant 1 (TOMAHTO)</P>
<P>25 istore_0 // Pop into local var 0: say = TOMAHTO</P>
<P>26 goto 2 // Branch unconditionally to 2, top of while loop</P>
<P>29 iconst_0 // Push constant 1 (TOMAYTO)</P>
<P>30 istore_0 // Pop into local var 0: say = TOMAYTO</P>
<P>31 goto 2 // Branch unconditionally to 2, top of while loop</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <CODE>argue()</CODE> method merely switches the value of <CODE>say</CODE> back and forth between <CODE>TOMAYTO</CODE> and <CODE>TOMAHTO</CODE>. Because the values of <CODE>TOMAYTO</CODE> and <CODE>TOMAHTO</CODE> were consecutive (<CODE>TOMAYTO</CODE> was a 0 and <CODE>TOMAHTO</CODE> was a 1), the <CODE>javac</CODE> compiler used a <CODE>tableswitch</CODE>. The <CODE>tableswitch</CODE> is a more efficient instruction than a <CODE>lookupswitch</CODE>, and the equivalent <CODE>lookupswitch</CODE> instruction would occupy 28 bytes--4 bytes more than <CODE>tableswitch</CODE>. </P>
<P>It turns out that even if <CODE>TOMAYTO</CODE> were a 0 and <CODE>TOMAHTO</CODE> were a 2, the <CODE>javac</CODE> compiler still would have used a <CODE>tableswitch</CODE>, because even with the extra default branch offset in there for a 1, the <CODE>tableswitch</CODE> instruction would occupy only 28 bytes--the same number of bytes as the equivalent <CODE>lookupswitch</CODE>. Both instructions occupy the same number of bytes, but <CODE>tableswitch</CODE> is more efficient, so it is used. As soon as you make <CODE>TOMAHTO</CODE> a 3, however, <CODE>javac</CODE> starts using a <CODE>lookupswitch</CODE>. This is because a <CODE>tableswitch</CODE> would now need two default branch offsets in its list (for 1 and 2), which would push its size up to 32 bytes. Thus, a <CODE>lookupswitch</CODE> now would require fewer bytes than a <CODE>tableswitch</CODE>--so <CODE>javac</CODE> chooses the <CODE>lookupswitch</CODE>.</P>
<P>The branch offsets for the case values cause the Java Virtual Machine to hop down to code that will change the value of the <CODE>say</CODE> local variable. The value of <CODE>say</CODE> will alternate between <CODE>TOMAYTO</CODE> and <CODE>TOMAHTO</CODE> indefinitely, until the user aborts the program, thereby calling the whole thing off. </P>
<P>To drive the <I>Saying Tomato</I> simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.</P>
<P><IMG SRC="fig16-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig16-1.gif" ALT="Figure 16-1"></P>
<H3><EM><P>On the CD-ROM</P>
</EM></H3><P> The CD-ROM contains the source code examples from this chapter in the <FONT FACE="Courier New">opcodes</FONT> directory. The <I>Saying Tomato</I> applet is contained in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/SayingTomato.html</FONT>. The source code for this applet is found alongside its class files, in the <FONT FACE="Courier New">applets/JVMSimulators</FONT> and <FONT FACE="Courier New">applets/JVMSimulators/COM/artima/jvmsim</FONT> directories.</P>
<H3><EM><P>The Resources Page</P>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/flow.html" tppabs="http://www.artima.com/insidejvm/flow.html">http://www.artima.com/insidejvm/flow.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap15.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap15.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap17.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap17.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
</TABLE>
<HR>
<P ALIGN=CENTER> <A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>
<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>
<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright © 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -