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

📄 tij0120.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
understand how RTTI works in Java, you must first know how type information is
represented at run time. This is accomplished through a special kind of object
called the <A NAME="Index1364"></A><A NAME="Index1365"></A><A NAME="Index1366"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Class
object,
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which contains information about the class. (This is sometimes called a <A NAME="Index1367"></A><A NAME="Index1368"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>meta-class.</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
In fact, the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is used to create all of the &#8220;regular&#8221; objects of your class.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There&#8217;s
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for each class that is part of your program. That is, each time you
write a new class, a single 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is also created (and stored, appropriately enough, in an identically
named 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">file).
At run time, when you want to make an object of that class, the <A NAME="Index1369"></A><A NAME="Index1370"></A>Java
Virtual Machine (JVM) that&#8217;s executing your program first checks to see
if the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for that type is loaded. If not, the JVM loads it by finding the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.class
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">file
with that name. Thus, a Java program isn&#8217;t completely loaded before it
begins, which is different from many traditional languages.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for that type is in memory, it is used to create all objects of that type.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
this seems shadowy or if you don&#8217;t really believe it, here&#8217;s a
demonstration program to prove it:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: SweetShop.java</font>
<font color="#009900">// Examination of the way the class loader works</font>

<font color="#0000ff">class</font> Candy {
  <font color="#0000ff">static</font> {
    System.out.println("Loading Candy");
  }
}

<font color="#0000ff">class</font> Gum {
  <font color="#0000ff">static</font> {
    System.out.println("Loading Gum");
  }
}

<font color="#0000ff">class</font> Cookie {
  <font color="#0000ff">static</font> {
    System.out.println("Loading Cookie");
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> SweetShop {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    System.out.println("inside main");
    <font color="#0000ff">new</font> Candy();
    System.out.println("After creating Candy");
    <font color="#0000ff">try</font> {
      Class.forName("Gum");
    } <font color="#0000ff">catch</font>(ClassNotFoundException e) {
      e.printStackTrace();
    }
    System.out.println(
      "After Class.forName(\"Gum\")");
    <font color="#0000ff">new</font> Cookie();
    System.out.println("After creating Cookie");
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Each
of the classes 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Candy</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Gum</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cookie</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
has a <A NAME="Index1371"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
clause that is executed as the class is loaded for the first time. Information
will be printed out to tell you when loading occurs for that class. In 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the object creations are spread out between print statements to help detect the
time of loading.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
particularly interesting line is:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Class.forName("Gum");</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
method is a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(to which all 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects belong). A 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is like any other object and so you can get and manipulate a handle to
it. (That&#8217;s what the loader does.) One of the ways to get a handle to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is <A NAME="Index1372"></A><A NAME="Index1373"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>forName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which takes a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
containing the textual name (watch the spelling and capitalization!) of the
particular class you want a handle for. It returns a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
output of this program for one JVM is:
</FONT><P></DIV>

<font color="#990000"><PRE>inside main
Loading Candy
After creating Candy
Loading Gum
After Class.forName("Gum")
Loading Cookie
After creating Cookie </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that each 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is loaded only when it&#8217;s needed, and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
initialization is performed upon class loading.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Interestingly
enough, a different JVM yielded:
</FONT><P></DIV>

<font color="#990000"><PRE>Loading Candy
Loading Cookie
inside main
After creating Candy
Loading Gum
After Class.forName("Gum")
After creating Cookie </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
appears that this JVM anticipated the need for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Candy</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cookie</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by examining the code in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but could not see 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Gum</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
because it was created by a call to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>forName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and not through a more typical call to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
While this JVM produces the desired effect because it does get the classes
loaded before they&#8217;re needed, it&#8217;s uncertain whether the behavior
shown is precisely correct.
</FONT><P></DIV>
<A NAME="Heading355"></A><H4 ALIGN=LEFT>
Class
literals
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
Java 1.1<A NAME="Index1374"></A>
you have a second way to produce the handle to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object: use the <A NAME="Index1375"></A><A NAME="Index1376"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>class
literal
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In the above program this would look like:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Gum.class;</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">which
is not only simpler, but also safer since it&#8217;s checked at compile time.
Because it eliminates the method call, it&#8217;s also more efficient.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Class
literals work with regular classes as well as interfaces, arrays, and primitive
types. In addition, there&#8217;s a standard field called <A NAME="Index1377"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TYPE</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that exists for each of the primitive wrapper classes. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>TYPE</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
field produces a handle to the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Class</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object for the associated primitive type, such that:
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
      <COL width="89">
      <COL width="95">
</COLGROUP>
<TR VALIGN="TOP">
<TH WIDTH=185 COLSPAN=2 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=CENTER><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">...
is equivalent to ...
</FONT><P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=89 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">boolean.class</FONT></TT><P></DIV>
</TD>
<TD WIDTH=95 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Boolean.TYPE</FONT></TT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=89 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">char.class</FONT></TT><P></DIV>
</TD>
<TD WIDTH=95 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Character.TYPE</FONT></TT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=89 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">byte.class</FONT></TT><P></DIV>
</TD>
<TD WIDTH=95 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Byte.TYPE</FONT></TT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=89 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">short.class</FONT></TT><P></DIV>
</TD>
<TD WIDTH=95 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">Short.TYPE</FONT></TT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=89 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">int.class</FONT></TT><P></DIV>
</TD>

⌨️ 快捷键说明

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