📄 chapter02.html
字号:
<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’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( )</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( )</B> increments the <B>static</B> data
<B>i</B>. You can call <B>incr( )</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( ) </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’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( )</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 “shepherd” 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’s the
program.</FONT><A NAME="fnB15" HREF="#fn15">[15]</A><FONT FACE="Georgia">
It prints out information about the system that it’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
‘<B>//</B>’, 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’ll need for the code in that file. Note that it is
“extra.” That’s because there’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’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’ll see a list of all the different class libraries that come with
Java. Select <B>java.lang</B>. Under “Class Index” you’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’ll see <B>System</B> and <B>Runtime</B>, which
are used in <B>Property.java</B>. There’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’t know the library where a particular class is, or if you want to
see all of the classes, you can select “Class Hierarchy” 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’s “find” function to find <B>Date. </B>When you do
you’ll see it listed as <B>java.util.Date</B>, which lets you know that
it’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’ve set in my Web
browser as the default starting page), select <B>java.lang</B> and then
<B>System</B>. You’ll see that the <B>System</B> class has several fields,
and if you select <B>out</B> you’ll discover that it’s a
<B>static</B> <B>PrintStream </B>object. Since it’s <B>static</B> you
don’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’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’re
interested in is <B>println( )</B>, which in effect means “print out
what I’m giving you to the console and end with a new line.” Thus,
in any Java program you write you can say
<B>System.out.println(“things”)</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’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’t do this.) That class must contain a
method called <B>main( )</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( )</B> is an array of <B>String</B>
objects. The <B>args</B> won’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( )</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’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( )</B>. If you consult the online documentation
using your Web browser, you’ll see that <B>getProperties( ) </B>is a
<B>static </B>method of class <B>System</B>. Because it’s <B>static</B>,
you don’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( )</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( )</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( )</B> are typical print statements. Note that to print multiple
<B>String</B> values, we simply separate them with ‘<B>+</B>’ signs.
However, there’s something strange going on here. The
‘<B>+</B>’ sign doesn’t mean addition when it’s used
with <B>String</B> objects. Normally, you wouldn’t ascribe any meaning to
‘<B>+</B>’ when you think of strings. However, the Java
<B>String</B> class is blessed with something called “operator
overloading.” That is, the ‘<B>+</B>’ 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 “concatenate these two
strings.”</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But that’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( )</B> and
<B>freeMemory( )</B> return <I>numerical values</I>, and not <B>String</B>
objects. What happens when you “add” 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 “added” 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’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( )</B> creates a <B>Runtime</B> object by calling the <B>static</B>
method <B>getRuntime( )</B> for the class <B>Runtime</B>. What’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’t need to concern you, since you can
use the objects without worrying about who’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’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’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’s easy. You don’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’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’t consider writing code to be the
only important activity – 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 + -