⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap12.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">idiv</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops two <FONT FACE="Courier New">int</FONT>s, divides them, and pushes the <FONT FACE="Courier New">int</FONT> result</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ldiv</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops two <FONT FACE="Courier New">long</FONT>s, divides them, and pushes the <FONT FACE="Courier New">long</FONT> result</TD></TR>
</TABLE>
<P>The remainder operation is performed on <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">long</FONT>s via the opcodes shown in Table 12-6. Theses opcodes cause the top two values to be popped from the stack. The value just beneath the topmost value is divided by the topmost value, and the remainder of that division is pushed back onto the stack. As with the division opcodes, integer remainder by zero throws an <FONT FACE="Courier New">ArithmeticException</FONT>.</P>
<P>Table 12-6. Integer remainder</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">irem</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops two <FONT FACE="Courier New">int</FONT>s, divides them, and pushes the <FONT FACE="Courier New">int</FONT> remainder</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lrem</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops two <FONT FACE="Courier New">long</FONT>s, divides them, and pushes the <FONT FACE="Courier New">long</FONT> remainder</TD></TR>
</TABLE>
<P>The opcodes shown in Table 12-7 perform arithmetic negation on <FONT FACE="Courier New">int</FONT>s and <FONT FACE="Courier New">long</FONT>s. The negation opcodes pop the top value from the stack, negate it, and push the result.</P>
<P>Table 12-7. Integer negation</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Opcode</STRONG></TD><TD VALIGN="TOP"><STRONG>Operand(s)</STRONG></TD><TD VALIGN="TOP"><STRONG>Description</STRONG></TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">ineg</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops an <FONT FACE="Courier New">int</FONT>, negates it, and pushes the result</TD></TR>
<TR><TD VALIGN="TOP"><FONT FACE="Courier New">lneg</FONT></TD><TD VALIGN="TOP">(none)</TD><TD VALIGN="TOP">pops a <FONT FACE="Courier New">long</FONT>, negates it, and pushes the result</TD></TR>
</TABLE>
<H3><EM><P>Prime Time</H3><STRONG>: A Simulation</P>
</I><P></STRONG>The <I>Prime Time</I> applet, shown in Figure 12-2, demonstrates a Java Virtual Machine executing a sequence of bytecodes that generates prime numbers. The applet is embedded in a web page on the CD-ROM in file <FONT FACE="Courier New">applets/PrimeTime.html</FONT>. The bytecode sequence in the simulation was generated by the <CODE>javac</CODE> compiler for the <FONT FACE="Courier New">findPrimes</FONT><CODE>()</CODE> 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 integer/ex1/PrimeFinder.java
<P>class PrimeFinder {</P>
<P>&nbsp;</P>
<P>    static void findPrimes() {</P>
<P>        </P>
<P>        int primeNum = 1;</P>
<P>        int numToCheck = 2;</P>
<P>&nbsp;</P>
<P>        for (;;) {</P>
<P>&nbsp;</P>
<P>            boolean foundPrime = true;</P>
<P>&nbsp;</P>
<P>            for (int divisor = numToCheck / 2; divisor </FONT> 1;</P>
<P>                --divisor) {</P>
<P>&nbsp;</P>
<P>                if (numToCheck % divisor == 0) {</P>
<P>                    foundPrime = false;</P>
<P>                    break;</P>
<P>                }</P>
<P>            }</P>
<P>&nbsp;</P>
<P>            if (foundPrime) {</P>
<P>                primeNum = numToCheck;</P>
<P>            }</P>
<P>&nbsp;</P>
<P>            ++numToCheck;</P>
<P>        }</P>
<P>    }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <FONT FACE="Courier New">findPrimes()</FONT> method places prime numbers, one at a time and in increasing numerical order, into the <FONT FACE="Courier New">primeNum</FONT> variable. To find the primes, it checks each positive integer in increasing numerical order starting with integer value two. It keeps the current number it is checking in the <FONT FACE="Courier New">numToCheck</FONT> variable. The outer for loop, a &quot;forever&quot; loop, keeps this process going indefinitely. To check a number, it divides the number by smaller integers looking for a zero remainder. If it encounters a zero remainder, then the number has integral factors other than one and itself and therefore isn韙 prime. </P>
<P>For each number to check, the <FONT FACE="Courier New">findPrimes()</FONT> method divides the number by two. The result of this integer division is the first value checked as a possible integral divisor for the number.</P>
<P>In the inner for loop, the <FONT FACE="Courier New">findPrimes()</FONT> method tries each number as a divisor between the result of the division by two and the divisor two. If the remainder of any of these divisions is zero, it breaks out of the inner for loop and skips to the next number to check. If it reaches divisor two and has found no divisor that yields a zero remainder, it has found the next prime number. It exits the inner for loop and sets <FONT FACE="Courier New">primeNum</FONT> equal to the number to check.</P>
<P>For example, when <FONT FACE="Courier New">numToCheck</FONT> is 10, <FONT FACE="Courier New">findPrimes()</FONT> first divides 10 by 2 to get the first divisor, 5. It then performs the remainder operation on 10 and 5 and discovers a zero remainder. So it breaks out of the inner for loop and sets numToCheck to 11. (It doesn韙 ever set <FONT FACE="Courier New">primeNum</FONT> to 10.) It divides 11 by 2 to get the first divisor to check, once again 5. It performs the remainder operation on 11 and integers 5, 4, 3, and 2, none of which yield a zero remainder. It completes the inner for loop, sets primeNum equal to 11, and continues on to check 12.</P>
<P>The bytecodes generated by <CODE>javac</CODE> for the <FONT FACE="Courier New">findPrimes</FONT><CODE>()</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_1      // Push int constant 1
<P> 1 istore_0      // Pop into local var 0: int primeNum = 1;</P>
<P> 2 iconst_2      // Push int constant 2</P>
<P> 3 istore_1      // Pop into local var 1: int numToCheck = 2;</P>
<P>&nbsp;</P>
<P>// The outer for loop (the "forever" loop) begins here:</P>
<P> 4 iconst_1      // Push int constant 1</P>
<P> 5 istore_2      // Pop into local var 2: boolean foundPrime = true;</P>
<P>&nbsp;</P>
<P>// The inner for loop begins here. First, initialize divisor.</P>
<P> 6 iload_1       // Push int in local var 1 (numToCheck)</P>
<P> 7 iconst_2      // Push int constant 2</P>
<P> 8 idiv          // Pop two ints, divide them, push int result</P>
<P>                 // Pop int into local var 3:</P>
<P>9 istore_3       // int divisor = numToCheck / 2;</P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -