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

📄 chapter02.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<B>static</B> variable. As indicated above, you can name it via an object, by
saying, for example, <B>st2.i</B>. You can also refer to it directly through its
class name, something you cannot do with a non-static member. (This is the
preferred way to refer to a <B>static</B> variable since it emphasizes that
variable&#8217;s <B>static</B> nature.)</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>StaticTest.i++;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>++</B> operator increments
the variable. At this point, both <B>st1.i</B> and <B>st2.i</B> will have the
value 48.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Similar logic applies to static
methods. You can refer to a static method either through an object as you can
with any method, or with the special additional syntax
<B>classname.method(&#160;)</B>. You define a static method in a similar
way:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> StaticFun {
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> incr() { StaticTest.i++; }
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that the
<B>StaticFun</B> method <B>incr(&#160;)</B> increments the <B>static</B> data
<B>i</B>. You can call <B>incr(&#160;)</B> in the typical way, through an
object:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>StaticFun sf = <font color=#0000ff>new</font> StaticFun();
sf.incr();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Or, because <B>incr(&#160;) </B>is
a static method, you can call it directly through its class:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>StaticFun.incr();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While <B>static</B>, when applied
to a data member, definitely changes the way the data is created (one for each
class vs. the non-<B>static </B>one for each object), when applied to a method
it&#8217;s not so dramatic. An important use of <B>static</B> for methods is to
allow you to call that method without creating an object. This is essential, as
we will see, in defining the <B>main(&#160;)</B> method that is the entry point
for running an application.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Like any method, a <B>static</B>
method can create or use named objects of its type, so a <B>static</B> method is
often used as a &#8220;shepherd&#8221; for a flock of instances of its own
type.</FONT><A NAME="_Toc375545233"></A><A NAME="_Toc408018434"></A><BR></P></DIV>
<A NAME="Heading79"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Your first Java program</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Finally, here&#8217;s the
program.</FONT><A NAME="fnB15" HREF="#fn15">[15]</A><FONT FACE="Georgia">
It prints out information about the system that it&#8217;s running on using
various methods of the <B>System</B> object from the Java standard library. Note
that an additional style of comment is introduced here: the
&#8216;<B>//</B>&#8217;, which is a comment until the end of the
line:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>// Property.java</font>
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Property {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    System.out.println(<font color=#0000ff>new</font> Date());
    Properties p = System.getProperties();
    p.list(System.out);
    System.out.println(<font color=#004488>"--- Memory Usage:"</font>);
    Runtime rt = Runtime.getRuntime();
    System.out.println(<font color=#004488>"Total Memory = "</font>
                       + rt.totalMemory()
                       + <font color=#004488>" Free Memory = "</font>
                       + rt.freeMemory());
  }
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At the beginning of each program
file, you must place the <B>import</B> statement to bring in any extra classes
you&#8217;ll need for the code in that file. Note that it is
&#8220;extra.&#8221; That&#8217;s because there&#8217;s a certain library of
classes that are automatically brought into every Java file: <B>java.lang</B>.
Start up your Web browser and look at the documentation from Sun. (If you
haven&#8217;t downloaded it from <I>java.sun.com</I> or otherwise installed the
Java documentation, do so now). If you look at the <B>packages.html</B> file,
you&#8217;ll see a list of all the different class libraries that come with
Java. Select <B>java.lang</B>. Under &#8220;Class Index&#8221; you&#8217;ll see
a list of all the classes that are part of that library. Since <B>java.lang</B>
is implicitly included in every Java code file, these classes are automatically
available. In the list, you&#8217;ll see <B>System</B> and <B>Runtime</B>, which
are used in <B>Property.java</B>. There&#8217;s no <B>Date</B> class listed in
<B>java.lang</B>, which means you must import another library to use that. If
you don&#8217;t know the library where a particular class is, or if you want to
see all of the classes, you can select &#8220;Class Hierarchy&#8221; in the Java
documentation. In a Web browser, this takes awhile to construct, but you can
find every single class that comes with Java. Then you can use the
browser&#8217;s &#8220;find&#8221; function to find <B>Date. </B>When you do
you&#8217;ll see it listed as <B>java.util.Date</B>, which lets you know that
it&#8217;s in the <B>util</B> library and that you must <B>import
java.util.*</B> in order to use <B>Date</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you look at the documentation
starting from the <B>packages.html</B> file (which I&#8217;ve set in my Web
browser as the default starting page), select <B>java.lang</B> and then
<B>System</B>. You&#8217;ll see that the <B>System</B> class has several fields,
and if you select <B>out</B> you&#8217;ll discover that it&#8217;s a
<B>static</B> <B>PrintStream </B>object. Since it&#8217;s <B>static</B> you
don&#8217;t need to create anything. The <B>out</B> object is always there and
you can just use it. What you can do with this <B>out</B> object is determined
by the type it is: a <B>PrintStream</B>. Conveniently, <B>PrintStream </B>is
shown in the description as a hyperlink, so if you click on that you&#8217;ll
see a list of all the methods you can call for <B>PrintStream</B>. There are
quite a few and these will be covered later in the book. For now all we&#8217;re
interested in is <B>println(&#160;)</B>, which in effect means &#8220;print out
what I&#8217;m giving you to the console and end with a new line.&#8221; Thus,
in any Java program you write you can say
<B>System.out.println(&#8220;things&#8221;)</B> whenever you want to print
something to the console.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The name of the class is the same
as the name of the file. When you&#8217;re creating a stand-alone program such
as this one, one of the classes in the file must have the same name as the file.
(The compiler complains if you don&#8217;t do this.) That class must contain a
method called <B>main(&#160;)</B> with the signature shown:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>public</B> keyword means
that the method is available to the outside world (described in detail in
Chapter 5). The argument to <B>main(&#160;)</B> is an array of <B>String</B>
objects. The <B>args</B> won&#8217;t be used in this program, but they need to
be there because they hold the arguments invoked on the command
line.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first line of the program is
quite interesting:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>System.out.println(<font color=#0000ff>new</font> Date());</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consider the argument: a
<B>Date</B> object is being created just to send its value to
<B>println(&#160;)</B>. As soon as this statement is finished, that <B>Date</B>
is unnecessary, and the garbage collector can come along and get it anytime. We
don&#8217;t need to worry about cleaning it up.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The second line calls
<B>System.getProperties(&#160;)</B>. If you consult the online documentation
using your Web browser, you&#8217;ll see that <B>getProperties(&#160;) </B>is a
<B>static </B>method of class <B>System</B>. Because it&#8217;s <B>static</B>,
you don&#8217;t need to create any objects in order to call the method; a
<B>static </B>method is always available whether an object of its class exists
or not. When you call <B>getProperties(&#160;)</B>, it<B> </B>produces the
system properties as an object of class <B>Properties</B>. The handle that comes
back is stored in a <B>Properties</B> handle called <B>p</B>. In line three, you
can see that the <B>Properties</B> object has a method called
<B>list(&#160;)</B> that sends its entire contents to a <B>PrintStream</B>
object that you pass as an argument.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The fourth and sixth lines in
<B>main(&#160;)</B> are typical print statements. Note that to print multiple
<B>String</B> values, we simply separate them with &#8216;<B>+</B>&#8217; signs.
However, there&#8217;s something strange going on here. The
&#8216;<B>+</B>&#8217; sign doesn&#8217;t mean addition when it&#8217;s used
with <B>String</B> objects. Normally, you wouldn&#8217;t ascribe any meaning to
&#8216;<B>+</B>&#8217; when you think of strings. However, the Java
<B>String</B> class is blessed with something called &#8220;operator
overloading.&#8221; That is, the &#8216;<B>+</B>&#8217; sign, only when used
with <B>String</B> objects, behaves differently from the way it does with
everything else. For <B>String</B>s, it means &#8220;concatenate these two
strings.&#8221;</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But that&#8217;s not all. If you
look at the statement:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>    System.out.println(<font color=#004488>"Total Memory = "</font>
                       + rt.totalMemory()
                       + <font color=#004488>" Free Memory = "</font>
                       + rt.freeMemory());</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>totalMemory(&#160;)</B> and
<B>freeMemory(&#160;)</B> return <I>numerical values</I>, and not <B>String</B>
objects. What happens when you &#8220;add&#8221; a numerical value to a
<B>String</B>? The compiler sees the problem and magically calls a method that
turns that numerical value (<B>int</B>, <B>float</B>, etc.) into a
<B>String</B>, which can then be &#8220;added&#8221; with the plus sign. This
<I>automatic type conversion</I> also falls into the category of operator
overloading.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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 <B>String</B> objects,
and you can&#8217;t overload operators for any of the code you
write.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The fifth line in
<B>main(&#160;)</B> creates a <B>Runtime</B> object by calling the <B>static</B>
method <B>getRuntime(&#160;)</B> for the class <B>Runtime</B>. What&#8217;s
returned is a handle to a <B>Runtime</B> object; whether this is a static object
or one created with <B>new</B> doesn&#8217;t need to concern you, since you can
use the objects without worrying about who&#8217;s responsible for cleaning them
up. As shown, the <B>Runtime</B> object can tell you information about memory
usage.</FONT><A NAME="_Toc375545234"></A><A NAME="_Toc408018435"></A><BR></P></DIV>
<A NAME="Heading80"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Comments and embedded
documentation<BR><A NAME="Index81"></A><A NAME="Index82"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are two types of comments in
Java. The first is the traditional C-style comment that was inherited by C++.
These comments begin with a <B>/*</B> and continue, possibly across many lines,
until a <B>*/</B>. Note that many programmers will begin each line of a
continued comment with a <B>*</B>, so you&#8217;ll often see:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>/* This is
*  A comment that continues
*  Across lines
*/</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Remember, however, that everything
inside the <B>/*</B> and <B>*/</B> is ignored so it&#8217;s no different to
say:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>/* This is a comment that
continues across lines */</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The second form of comment comes
from C++. It is the single-line comment, which starts at a <B>//</B> and
continues until the end of the line. This type of comment is convenient and
commonly used because it&#8217;s easy. You don&#8217;t need to hunt on the
keyboard to find <B>/</B> and then <B>*</B> (you just press the same key twice),
and you don&#8217;t need to close the comment. So you will often
see:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>// this is a one-line comment</font></PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><A NAME="_Toc375545235"></A><A NAME="_Toc408018436"></A><BR></P></DIV>
<A NAME="Heading81"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Comment documentation</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the thoughtful parts of the
Java language is that the designers didn&#8217;t consider writing code to be the
only important activity &#8211; they also thought about documenting it. Possibly
the biggest problem with documenting code has been maintaining that
documentation. If the documentation and the code are separate, it becomes a
hassle to change the documentation every time you change the code. The solution
seems simple: link the code to the documentation. The easiest way to do this is
to put everything in the same file. To complete the picture, however, you need a
special comment syntax to mark special documentation and a tool to extract those
comments and put them in a useful form. This is what Java has
done.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The tool to extract the comments is
called <I>javadoc.</I> It uses some of the technology from the Java compiler to
look for special comment tags you

⌨️ 快捷键说明

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