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

📄 compiling.doc.html

📁 一本关于java方面的书籍 英文html形式 关于Java虚拟机
💻 HTML
📖 第 1 页 / 共 5 页
字号:
might be compiled to
<p><a name="4854"></a>
 <i>Method </i><code>void</code> <code>createBuffer()<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td>          <i>bipush 100
</i><br><td><i>// Push bufsz
</i>
<tr><td>          <i>   2 
</i><br><td>          <i>istore_2
</i><br><td><i>// Store bufsz in local 2
</i>
<tr><td>          <i>   3 
</i><br><td>          <i>bipush 12
</i><br><td><i>// Push value
</i>
<tr><td>          <i>   5 
</i><br><td>          <i>istore_3
</i><br><td><i>// Store value in local 3
</i>
<tr><td>          <i>   6
</i><br><td>          <i>iload_2
</i><br><td><i>// Push bufsz...
</i>
<tr><td>          <i>   7
</i><br><td>          <i>newarray </i><code>int
</code><br><td><i>// ...and create new array of int
</i>
<tr><td>          <i>   9 
</i><br><td>          <i>astore_1
</i><br><td><i>// Store new array in buffer
</i>
<tr><td>          <i>  10 
</i><br><td>          <i>aload_1
</i><br><td><i>// Push buffer
</i>
<tr><td>          <i>  11 
</i><br><td>          <i>bipush 10
</i><br><td><i>// Push constant 10
</i>
<tr><td>          <i>  13 
</i><br><td>          <i>iload_3
</i><br><td><i>// Push value
</i>
<tr><td>          <i>  14 
</i><br><td>          <i>iastore
</i><br><td><i>// Store value at buffer[10]
</i>
<tr><td>          <i>  15 
</i><br><td>          <i>aload_1
</i><br><td><i>// Push buffer
</i>
<tr><td>          <i>  16 
</i><br><td>          <i>bipush 11
</i><br><td><i>// Push constant 11
</i>
<tr><td>          <i>  18 
</i><br><td>          <i>iaload
</i><br><td><i>// Push value at buffer[11]
</i>
<tr><td>          <i> &#32; 19 
</i><br><td>          <i>istore_3
</i><br><td><i>// ...and store it in value
</i>
<tr><td>          <i>  20
</i><br><td>          <i>return
</i><br><td><i>
</i>
</Table><br><br></code><p>
<a name="6189"></a>
The <i>anewarray</i> instruction is used to create a one-dimensional array of object references:<p>
<pre><br><a name="6192"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>void createThreadArray() {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	Thread threads[];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	int count = 10;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	threads = new Thread[count];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	threads[0] = new Thread();
</code><a name="6197"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="6187"></a>
becomes
<p><a name="6188"></a>
 <i>Method </i><code>void createThreadArray()<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td>          <i>bipush 10
</i><br><td><i>// Push 10...
</i>
<tr><td>          <i>   2 
</i><br><td>          <i>istore_2
</i><br><td><i>// ...and initialize count to that
</i>
<tr><td>          <i>   3 
</i><br><td>          <i>iload_2
</i><br><td><i>// Push count, used by anewarray
</i>
<tr><td>          <i>   4 
</i><br><td>          <i>anewarray 
class #1 
</i><br><td><i>// Create new array of class Thread
</i>
<tr><td>          <i>   7 
</i><br><td>          <i>astore_1
</i><br><td><i>// Store new array in threads
</i>
<tr><td>          <i>   8 
</i><br><td>          <i>aload_1
</i><br><td><i>// Load value of threads on stack
</i>
<tr><td>          <i>   9 
</i><br><td>          <i>iconst_0
</i><br><td><i>// Load 0 into stack
</i>
<tr><td>          <i>  10 
</i><br><td>          <i>new #1 
</i><br><td><i>// Create instance of class Thread
</i>
<tr><td>          <i>  13 
</i><br><td>          <i>dup
</i><br><td><i>// Make duplicate reference...
</i>
<tr><td>          <i>  14 
</i><br><td>          <i>invokespecial 
#5 
</i><br><td><i>// ...to pass to initialization method
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// Method java.lang.Thread.&lt;init&gt;()V
</i>
<tr><td>          <i>  17 
</i><br><td>          <i>aastore
</i><br><td><i>// Store new Thread in array at 0
</i>
<tr><td>          <i>  18 
</i><br><td>          <i>return
</i><br><td><i>
</i>
</Table><br><br></code><p>
<a name="5674"></a>
The<i> anewarray</i> instruction can also be used to create the first dimension of a multidimensional array. Alternatively, the <i>multianewarray</i> instruction can be used to create several dimensions at once. For example, the three-dimensional array in the following:<p>
<pre><br><a name="6235"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>int[][][] create3DArray() {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	int grid[][][];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	grid = new int[10][5][];
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	return grid;
</code><a name="12919"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br></pre><a name="12921"></a>
is created by
<p><a name="6227"></a>
 <i>Method </i><code>int</code> <code>create3DArray()[][][]<p><Table Border="0">
<tr><td>          <i>   0 
</i><br><td>          <i>bipush 10
</i><br><td><i>// Push 10 (dimension one)
</i>
<tr><td>          <i>   2 
</i><br><td>          <i>iconst_5
</i><br><td><i>// Push 5 (dimension two)
</i>
<tr><td>          <i>   3 
</i><br><td>          <i>multianewarra
y #1 dim #2 
</i><br><td><i>// Class [[[I, a three
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// dimensional int array;
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// only create first two 
</i>
<tr><td>          <i>
</i><br><td>          <i>
</i><br><td><i>// dimensions
</i>
<tr><td>          <i>   7 
</i><br><td>          <i>astore_1
</i><br><td><i>// Store new array...
</i>
<tr><td>          <i>   8 
</i><br><td>          <i>aload_1
</i><br><td><i>// ...then prepare to return it
</i>
<tr><td>          <i>   9 
</i><br><td>          <i>areturn
</i><br><td><i>
</i>
</Table><br><br></code><p>
<a name="5678"></a>
The first operand of the <i>multianewarray</i> instruction is the constant pool index to the 
array class type to be created. The second is the number of dimensions of that array type 
to actually create. The <i>multianewarray</i> instruction can be used to create all the dimensions of the type, as the code for <code>create3DArray</code> shows. Note that the multidimensional 
array is just an object, and so is loaded and returned by an <i>aload_1</i> and <i>areturn</i> instruction, respectively. For information about array class names, see <a href="ClassFile.doc.html#1221">&#167;4.4.1</a>.
<p><a name="6342"></a>
All arrays have associated lengths, which are accessed via the <i>arraylength</i> instruction.<p>
<a name="4095"></a>
<hr><h2>7.10	 Compiling Switches</h2>
<a name="25132"></a>
Java's <code>switch</code> statements are compiled using the <i>tableswitch</i> and <i>lookupswitch</i> 
instructions. The <i>tableswitch</i> instruction is used when the cases of the <code>switch</code> can be 
efficiently represented as indices into a table of target offsets. The <code>default</code> target of 
the <code>switch</code> is used if the value of the expression of the <code>switch</code> falls outside the range of 
valid indices. For instance,
<p><pre><br><a name="25321"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>int chooseNear(int i) {int chooseNear(int i) {
</code></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<code>	switch (i) {
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    case 0: 			return 0;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    case 1: 			return 1;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    case 2: 			return 2;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	    default:			return -1;
</code>&nbsp;&nbsp;&nbsp;&nbsp;<code>	}
</code><a name="25328"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>}
</code><br><br><a name="25317"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>
</code><br><a name="25318"></a>&nbsp;&nbsp;&nbsp;&nbsp;<code>
</code></pre><a name="25319"></a>
compiles to
<p><a name="4512"></a>
 <i>Method </i><code>int</code> <code>chooseNear(int)<p><Table Border="0">
<tr><td>          <i>   0
</i><br><td>          <i>iload_1
</i><br><td><i>// Load local 1 (argument i)
</i>
<tr><td>          <i>   1 
</i><br><td>          <i>tableswitch 0 to 
2: 
</i><br><td><i>// Valid indices are 0 through 2
</i>
<tr><td>          <i>
</i><br><td>          <i>0: 28
</i><br><td><i>// If i is 0, continue at 28
</i>
<tr><td>          <i>
</i><br><td>          <i>1: 30
</i><br><td><i>// If i is 1, continue at 30
</i>
<tr><td>          <i>
</i><br><td>          <i>2: 32
</i><br><td><i>// If i is 2, continue at 32
</i>
<tr><td>          <i>
</i><br><td>          <i>default:34
</i><br><td><i>// Otherwise, continue at 34
</i>
<tr><td>          <i>  28 
</i><br><td>          <i>iconst_0
</i><br><td><i>// i was 0; push int 0...
</i>
<tr><td>          <i>  29 
</i><br><td>          <i>ireturn
</i><br><td><i>// ...and return it
</i>
<tr><td>          <i>  30 
</i><br><td>          <i>iconst_1
</i><br><td><i>// i was 1; push int 1...
</i>
<tr><td>          <i>  31 
</i><br><td>          <i>ireturn
</i><br><td><i>// ...and return it
</i>
<tr><td>          <i>  32 
</i><br><td>          <i>iconst_2
</i><br><td><i>// i was 2; push int 2...
</i>
<tr><td>          <i>  33 
</i><br><td>          <i>ireturn
</i><br><td><i>// ...and return it
</i>
<tr><td>          <i>  34 
</i><br><td>          <i>iconst_m1
</i><br><td><i>// otherwise push int -1...
</i>
<tr><td>          <i>  35 
</i><br><td>          <i>ireturn
</i><br><td><i>// ...and return it
</i>
</Table><br><br></code><p>
<a name="745

⌨️ 快捷键说明

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