📄 tij0038.html
字号:
Date());
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Consider
the argument: a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Date</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is being created just to send its value to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>println( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
As soon as this statement is finished, that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Date</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is unnecessary, and the garbage collector can come along and get it anytime. We
don’t need to worry about cleaning it up.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second line calls
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.getProperties( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If you consult the online documentation using your Web browser, you’ll
see that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getProperties( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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">method
of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Because it’s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you don’t need to create any objects in order to call the method; a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method
is always available whether an object of its class exists or not. When you call
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getProperties( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">produces
the system properties as an object of class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The handle that comes back is stored in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>p</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In line three, you can see that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Properties</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object has a method called
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that sends its entire contents to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object that you pass as an argument.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fourth and sixth lines in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
are typical print statements. Note that to print multiple
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
values, we simply separate them with ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’
signs. However, there’s something strange going on here. The ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’
sign doesn’t mean addition when it’s used with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. Normally, you wouldn’t ascribe any meaning to ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’
when you think of strings. However, the Java
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is blessed with something called “operator overloading.” That
is, the ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’
sign, only when used with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, behaves differently from the way it does with everything else. For
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
it means “concatenate these two strings.”
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">But
that’s not all. If you look at the statement:
</FONT><P></DIV>
<font color="#990000"><PRE> System.out.println("Total Memory = "
+ rt.totalMemory()
+ " Free Memory = "
+ rt.freeMemory()); </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>totalMemory( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>freeMemory( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
return
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>numerical
values
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and not
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. What happens when you “add” a numerical value to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">?
The compiler sees the problem and magically calls a method that turns that
numerical value (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>float</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
etc.) into a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which can then be “added” with the plus sign. This
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>automatic
type conversion
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also falls into the category of operator overloading.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Much
of the Java literature states vehemently that operator overloading (a feature
in C++) is bad, and yet here it is! However, this is wired into the compiler,
only for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, and you can’t overload operators for any of the code you write.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fifth line in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
creates a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object by calling the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getRuntime( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for the class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
What’s returned is a handle to a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object; whether this is a static object or one created with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
doesn’t need to concern you, since you can use the objects without
worrying about who’s responsible for cleaning them up. As shown, the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object can tell you information about memory usage.
</FONT><a name="_Toc375545234"></a><a name="_Toc408018435"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn15" HREF="#fnB15">[15]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
Some programming environments will flash programs up on the screen and close
them before you've had a chance to see the results. You can put in the
following bit of code at the end of
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
to pause the output:
</FONT><P></DIV>
<font color="#990000"><PRE> <font color="#0000ff">try</font> {
Thread.currentThread().sleep(5 * 1000);
} <font color="#0000ff">catch</font>(InterruptedException e) {}</PRE></font><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=2 COLOR="Black">
}
</FONT></TT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">This
will pause the output for five seconds. This code involves concepts that will
not be introduced until much later in the book, so you won’t understand
it until then, but it will do the trick.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0037.html">Prev</a> | <a href="tij0039.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -