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

📄 chap07.html

📁 Inside the java virtualMachine,深入研究java虚拟机
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<P>    }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>Running the <FONT FACE="Courier New">Example4</FONT> application yields the following output:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT FACE="Courier New"><P>Example3 was initialized.</P>
<P>Grrrr!</P>
<P>Woof, woof, world!</P>
</FONT><P><FONT FACE="Courier New">end</FONT></P></PRE>
<P>Had <FONT FACE="Courier New">Angry</FONT> been initialized, the string <FONT FACE="Courier New">"Angry was initialized."</FONT> would have been written to the standard output. Likewise, had <FONT FACE="Courier New">Dog</FONT> been initialized, the string <FONT FACE="Courier New">"Dog was initialized."</FONT> would have been written to the standard output. As you can see from the above output, neither interface <FONT FACE="Courier New">Angry</FONT> or class <FONT FACE="Courier New">Dog</FONT> were ever initialized during the execution of the <FONT FACE="Courier New">Example3</FONT> application.</P>
<P>For more information about this special treatment of static final variables, see Chapter 8, &quot;The Linking Model.&quot;</P>
<H3><EM><P>The Lifetime of an Object</P>
</EM></H3><P>Once a class has been loaded, linked, and initialized, it is ready for use. The program can access its static fields, invoke its static methods, or create instances of it. This section describes class instantiation and initialization, activities that take place at the beginning of an object韘 lifetime, and garbage collection and finalization, activities that mark the end of an object韘 lifetime.</P>
<H3><P>Class Instantiation</P>
</H3><P>In Java programs, classes can be instantiated explicitly or implicitly. The three ways a class can be instantiated explicitly are with the <FONT FACE="Courier New">new</FONT> operator, by invoking <FONT FACE="Courier New">newInstance()</FONT>on a <FONT FACE="Courier New">Class</FONT> object, or by invoking <FONT FACE="Courier New">clone()</FONT> on any existing object. Here is an example showing each way to create a new class instance:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">// On CD-ROM in file classlife/ex4/Example4.java
<P>class Example4 implements Cloneable {</P>
<P>&nbsp;</P>
<P>    Example4() {</P>
<P>        System.out.println("Created by invoking newInstance()");</P>
<P>    }</P>
<P>&nbsp;</P>
<P>    Example4(String msg) {</P>
<P>        System.out.println(msg);</P>
<P>    }</P>
<P>&nbsp;</P>
<P>    public static void main(String[] args)</P>
<P>        throws ClassNotFoundException, InstantiationException,</P>
<P>        IllegalAccessException, CloneNotSupportedException {</P>
<P>&nbsp;</P>
<P>        // Create a new Example4 object with the new operator</P>
<P>        Example4 obj1 = new Example4("Created with new.");</P>
<P>&nbsp;</P>
<P>        // Get a reference to the Class instance for Example4, then</P>
<P>        // invoke newInstance() on it to create a new Example4 object</P>
<P>        Class myClass = Class.forName("Example4"); </P>
<P>        Example4 obj2 = (Example4) myClass.newInstance();</P>
<P>&nbsp;</P>
<P>        // Make an identical copy of the the second Example4 object</P>
<P>        Example4 obj3 = (Example4) obj2.clone();</P>
<P>    }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>When executed, the <FONT FACE="Courier New">Example4</FONT> application prints this output:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">Created with new.
<P>Created by invoking newInstance()</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>Besides the three ways listed above to explicitly instantiate objects in Java source code, there are several situations in which objects will be instantiated implicitly--without an explicit <FONT FACE="Courier New">new</FONT>, <FONT FACE="Courier New">newInstance()</FONT>, or <FONT FACE="Courier New">clone()</FONT> appearing in the source.</P>
<P>Possibly the first implicitly instantiated objects of any Java application are the <FONT FACE="Courier New">String</FONT> objects that hold the command line arguments. References to these objects, one for each command-line argument, are delivered in the <FONT FACE="Courier New">String</FONT> array passed as the sole parameter to the <FONT FACE="Courier New">main()</FONT> method of every application.</P>
<P>Two other ways a class can be instantiated implicitly involve the process of class loading. First, for every type a Java Virtual Machine loads, it implicitly instantiates a new <FONT FACE="Courier New">Class</FONT> object to represent that type. Second, when the Java Virtual Machine loads a class that contains <FONT FACE="Courier New">CONSTANT_String_info</FONT> entries in its constant pool, it may instantiate new <FONT FACE="Courier New">String</FONT> objects to represent those constant string literals. The process of transforming a <FONT FACE="Courier New">CONSTANT_String_info</FONT> entry in the method area to a <FONT FACE="Courier New">String</FONT> instance on the heap is part of the process of constant pool resolution. This process is described in detail in Chapter 8, &quot;The Linking Model.&quot;</P>
<P>Another way objects can be created implicitly is through the process of evaluating an expression that involves the string concatenation operator. If such an expression is not a compile-time constant, intermediate <FONT FACE="Courier New">String</FONT> and <FONT FACE="Courier New">StringBuffer</FONT> objects will be created in the process of evaluating the expression. Here韘 an example:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New">// On CD-ROM in file classlife/ex5/Example5.java
<P>class Example5 {</P>
<P>&nbsp;</P>
<P>    public static void main(String[] args) {</P>
<P>&nbsp;</P>
<P>        if (args.length &lt; 2) {</P>
<P>            System.out.println("Must enter any two args.");</P>
<P>            return;</P>
<P>        }</P>
<P>&nbsp;</P>
<P>        System.out.println(args[0] + args[1]);</P>
<P>    }</P>
<P>}</P>
</FONT><FONT SIZE="2"><P>&nbsp;</P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P><FONT FACE="Courier New">javac</FONT> generates these bytecodes for <FONT FACE="Courier New">Example5</FONT>韘 <FONT FACE="Courier New">main()</FONT> method:</P>
<PRE><P><FONT FACE="Courier New">begin</FONT></P>
<FONT SIZE="2"><P></FONT><FONT FACE="Courier New"> 0 aload_0      // Push the objref from loc var 0 (args)
<P> 1 arraylength  // Pop arrayref, calc array length, push int length</P>
<P> 2 iconst_2     // Push int constant 2</P>
<P>                // Pop 2 ints, compare, branch if (length </FONT> 2) to</P>
<P> 3 if_icmpge 15 // offset 15.</P>
<P>                // Push objref from System.out</P>
<P> 6 getstatic #11 &lt;Field java.io.PrintStream out</FONT></P>
<P>                // Push objref of string literal</P>
<P> 9 ldc #1 &lt;String "Must enter any two args."</FONT></P>
<P>                // Pop objref to String param, objref to System.out,</P>
<P>                // invoke println()</P>
<P>11 invokevirtual #12 &lt;Method void println(java.lang.String)</FONT></P>
<P>14 return       // Return void from main()</P>
<P>                // Push objref from System.out</P>
<P>15 getstatic #11 &lt;Field java.io.PrintStream out</FONT></P>
<P>&nbsp;</P>
<P>// The string concatenation operation begins here</P>
<P>                // Allocate mem for new StringBuffer object, and</P>
<P>                // initialize mem to default initial values, push</P>
<P>                // objref to new object</P>
<P>18 new #6 &lt;Class java.lang.StringBuffer</FONT></P>
<P>21 dup          // Duplicate objref to StringBuffer object</P>
<P>22 aload_0      // Push ref from loc var 0 (args)</P>
<P>23 iconst_0     // Push int constant 0</P>
<P>                // Pop int, arrayref, push String at arrayref[int],</P>
<P>24 aaload       // which is args[0]</P>
<P>                // Pop objref, invoke String's class method</P>
<P>                // valueOf(), passing it the objref to the args[0]</P>
<P>                // String object. valueOf() calls toString() on the</P>
<P>                // ref, and returns (and pushes) the result, which</P>
<P>                // happens to be the original args[0] String. In this</P>
<P>                // case, the stack will look precisely the same</P>
<P>                // before and after this instruction is executed.</P>
<P>                // Thus here, the 1.1 javac compiler has</P>
<P>                // over-enthusiastically generated an unnecessary</P>
<P>                // instruction.</P>
<P>25 invokestatic #14 &lt;Method java.lang.String valueOf(</P>
<P>        java.lang.Object)</FONT></P>
<P>                // Pop objref to args[0] String, objref of the</P>
<P>                // StringBuffer object, invoke &lt;init</FONT>() method on the</P>
<P>                // StringBuffer object passing the args[0] objref as</P>
<P>                // the only parameter.</P>
<P>28 invokespecial #9 &lt;Method java.lang.StringBuffer(java.lang.String)</FONT></P>
<P>31 aload_0      // Push objref from loc var 0 (args)</P>
<P>32 iconst_1     // Push int constant 1</P>

⌨️ 快捷键说明

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