📄 tij0038.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0037.html">Prev</a> | <a href="tij0039.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
Your
first Java program
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Finally,
here’s the program.
</FONT><A NAME="fnB15" HREF="#fn15">[15]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
It prints out information about the system that it’s running on using
various methods of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object from the Java standard library. Note that an additional style of comment
is introduced here: the ‘
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>//</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">’,
which is a comment until the end of the line:
</FONT><P></DIV>
<font color="#990000"><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("--- Memory Usage:");
Runtime rt = Runtime.getRuntime();
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">At
the beginning of each program file, you must place the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.lang</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Start up your Web browser and look at the documentation from Sun. (If you
haven’t downloaded it from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>java.sun.com</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or otherwise installed the Java documentation, do so now). If you look at the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>packages.html</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file, you’ll see a list of all the different class libraries that come
with Java. Select
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.lang</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Under “Class Index” you’ll see a list of all the classes that
are part of that library. Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.lang</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is implicitly included in every Java code file, these classes are automatically
available. In the list, you’ll see
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Runtime</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which are used in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Property.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
There’s no
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Date</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class listed in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.lang</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Date.
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you do you’ll see it listed as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util.Date</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which lets you know that it’s in the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
library and that you must
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import
java.util.*
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in order to use
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Date</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
you look at the documentation starting from the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>packages.html</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
file (which I’ve set in my Web browser as the default starting page),
select
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.lang</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and then
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You’ll see that the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class has several fields, and if you select
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you’ll discover that it’s a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">object.
Since 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 anything. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is always there and you can just use it. What you can do with this
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>out</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object is determined by the type it is: a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Conveniently,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>PrintStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
There are quite a few and these will be covered later in the book. For now all
we’re interested in is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>println( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>System.out.println(“things”)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
whenever you want to print something to the console.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with the signature shown:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">public
static void main(String[] args) {
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword means that the method is available to the outside world (described in
detail in Chapter 5). The argument to
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is an array of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>args</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
won’t be used in this program, but they need to be there because they
hold the arguments invoked on the command line.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first line of the program is quite interesting:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">System.out.println(new
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -