📄 chap11.html
字号:
<P> // now (a + b), an int.</P>
<P> 7 i2b // Convert int result to byte (result still</P>
<P> // occupies 32 bits).</P>
<P> 8 istore_2 // Pop into local variable 3, which is</P>
<P> // byte c: byte c = (byte) (a + b);</P>
<P> 9 iload_2 // Push the value of c so it can be returned.</P>
<P>10 ireturn // Proudly return the result of the addition: return c;</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<I><STRONG><P>Conversion Diversion: A Simulation</P>
</I></STRONG><H3><P></H3>The <I>Conversion Diversion</I> applet, shown in Figure 11-1, demonstrates a Java Virtual Machine executing a sequence of bytecodes. The applet is embedded in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/ConversionDiversion.html</FONT>. The bytecode sequence in the simulation was generated by <CODE>javac</CODE> for the <FONT FACE="Courier New">Convert()</FONT> method of the class shown below: </P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">// On CD-ROM in file opcodes/ex1/Diversion.java
<P>class Diversion {</P>
<P> </P>
<P> static void Convert() {</P>
<P> </P>
<P> byte imByte = 0;</P>
<P> int imInt = 125;</P>
<P> </P>
<P> for (;;) {</P>
<P> ++imInt;</P>
<P> imByte = (byte) imInt;</P>
<P> </P>
<P> imInt *= -1;</P>
<P> imByte = (byte) imInt;</P>
<P> imInt *= -1;</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 <FONT FACE="Courier New">Convert()</FONT> 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 int constant 0.
<P> 1 istore_0 // Pop to local variable 0, which is</P>
<P> // imByte: byte imByte = 0;</P>
<P> 2 bipush 125 // Expand byte constant 125 to int and push.</P>
<P> 4 istore_1 // Pop to local variable 1, which</P>
<P> // is imInt: int imInt = 125;</P>
<P> 5 iinc 1 1 // Increment local variable 1 (imInt) by 1: ++imInt;</P>
<P> 8 iload_1 // Push local variable 1 (imInt).</P>
<P> 9 i2b // Truncate and sign extend top of stack so it</P>
<P> // has a valid byte value.</P>
<P>10 istore_0 // Pop to local variable 0 (imByte):</P>
<P> // imByte = (byte) imInt;</P>
<P>11 iload_1 // Push local variable 1 (imInt) again.</P>
<P>12 iconst_m1 // Push integer -1.</P>
<P>13 imul // Pop top two ints, multiply, push result.</P>
<P>14 istore_1 // Pop result of multiply to local variable 1 (imInt):</P>
<P> // imInt *= -1;</P>
<P>15 iload_1 // Push local variable 1 (imInt).</P>
<P>16 i2b // Truncate and sign extend top of stack so it has</P>
<P> // a valid byte value.</P>
<P>17 istore_0 // Pop to local variable 0 (imByte):</P>
<P> // imByte = (byte) imInt;</P>
<P>18 iload_1 // Push local variable 1 (imInt) again.</P>
<P>19 iconst_m1 // Push integer -1.</P>
<P>20 imul // Pop top two ints, multiply, push result.</P>
<P>21 istore_1 // Pop result of multiply to local variable 1 (imInt):</P>
<P> // imInt *= -1;</P>
<P>22 goto 5 // Jump back to the iinc instruction: for (;;) {}</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <FONT FACE="Courier New">Convert()</FONT> method demonstrates the manner in which the Java Virtual Machine converts from <FONT FACE="Courier New">int</FONT> to <FONT FACE="Courier New">byte</FONT>. <CODE>imInt</CODE> starts out as 125. Each pass through the while loop, it is incremented and converted to a <FONT FACE="Courier New">byte</FONT>. Then it is multiplied by -1 and again converted to a <FONT FACE="Courier New">byte</FONT>. The simulation quickly shows what happens at the edges of the valid range for the <FONT FACE="Courier New">byte</FONT> type. </P>
<P>The maximum value for a <FONT FACE="Courier New">byte</FONT> is 127. The minimum value is -128. Values of type <FONT FACE="Courier New">int</FONT> that are within this range convert directly to <FONT FACE="Courier New">byte</FONT>. As soon as the <FONT FACE="Courier New">int</FONT> gets beyond the valid range for <FONT FACE="Courier New">byte</FONT>, however, things get interesting. </P>
<P>The Java Virtual Machine converts an <FONT FACE="Courier New">int</FONT> to a <FONT FACE="Courier New">byte</FONT> by truncating and sign extending. The highest order bit, the "sign bit," of <FONT FACE="Courier New">long</FONT>s, <FONT FACE="Courier New">int</FONT>s, <FONT FACE="Courier New">short</FONT>s, and <FONT FACE="Courier New">byte</FONT>s indicate whether or not the integer value is positive or negative. If the sign bit is zero, the value is positive. If the sign bit is one, the value is negative. Bit 7 of a <FONT FACE="Courier New">byte</FONT> value is its sign bit. To convert an <FONT FACE="Courier New">int</FONT> to a <FONT FACE="Courier New">byte</FONT>, bit 7 of the <FONT FACE="Courier New">int</FONT> is copied to bits 8 through 31. This produces an <FONT FACE="Courier New">int</FONT> that has the same numerical value that the <FONT FACE="Courier New">int</FONT>'s lowest order byte would have if it were interpreted as a <FONT FACE="Courier New">byte</FONT> type. After the truncation and sign extension, the <FONT FACE="Courier New">int</FONT> will contain a valid <FONT FACE="Courier New">byte</FONT> value. </P>
<P>The simulation applet shows what happens when an <FONT FACE="Courier New">int</FONT> that is just beyond the valid range for <FONT FACE="Courier New">byte</FONT> types gets converted to a <FONT FACE="Courier New">byte</FONT>. For example, when the <FONT FACE="Courier New">imInt</FONT> variable has a value of 128 (0x00000080) and is converted to <FONT FACE="Courier New">byte</FONT>, the resulting <FONT FACE="Courier New">byte</FONT> value is -128 (0xffffff80). Later, when the <FONT FACE="Courier New">imInt</FONT> variable has a value of -129 (0xffffff7f) and is converted to <FONT FACE="Courier New">byte</FONT>, the resulting <FONT FACE="Courier New">byte</FONT> value is 127 (0x0000007f). </P>
<P>To drive the <I>Conversion Diversion</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="fig11-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig11-1.gif" ALT="Figure 11-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>Conversion Diversion</I> applet is contained in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/ConversionDiversion.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/typeconv.html" tppabs="http://www.artima.com/insidejvm/typeconv.html">http://www.artima.com/insidejvm/typeconv.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="chap10.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap10.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="chap12.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap12.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 + -