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

📄 chap02.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
type of the reference must be correct, however. If the argument is supposed to
be a <B>String</B>, what you pass in must be a string.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I56' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I57>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consider a method that takes a
<B>String</B> as its argument. Here is the definition, which must be placed
within a class definition for it to be compiled:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> storage(String s) {
  <font color=#0000ff>return</font> s.length() * 2;
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This method tells you how many bytes are
required to hold the information in a particular <B>String. </B>(Each <B>char
</B>in a <B>String </B>is 16 bits, or two bytes, long, to support Unicode
characters.) The argument is of type <B>String</B> and is called <B>s</B>. Once
<B>s</B> is passed into the method, you can treat it just like any other object.
(You can send messages to it.) Here, the <B>length(&#160;)</B> method is called,
which is one of the methods for <B>String</B>s; it returns the number of
characters in a string. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I57' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I58>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can also see the use of the
<B>return</B> keyword, which does two things. First, it means &#8220;leave the
method, I&#8217;m done.&#8221; Second, if the method produces a value, that
value is placed right after the <B>return</B> statement. In this case, the
return value is produced by evaluating the expression <B>s.length(&#160;) *
2</B>. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I58' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I59>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can return any type you want, but if
you don&#8217;t want to return anything at all, you do so by indicating that the
method returns <B>void</B>. Here are some examples:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>boolean</font> flag() { <font color=#0000ff>return</font> <font color=#0000ff>true</font>; }
<font color=#0000ff>float</font> naturalLogBase() { <font color=#0000ff>return</font> 2.718f; }
<font color=#0000ff>void</font> nothing() { <font color=#0000ff>return</font>; }
<font color=#0000ff>void</font> nothing2() {}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When the return type is <B>void</B>, then
the <B>return</B> keyword is used only to exit the method, and is therefore
unnecessary when you reach the end of the method. You can return from a method
at any point, but if you&#8217;ve given a non-<B>void </B>return type then the
compiler will force you (with error messages) to return the appropriate type of
value regardless of where you return.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I59' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I60>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">At this point, it can look like a program
is just a bunch of objects with methods that take other objects as arguments and
send messages to those other objects. That is indeed much of what goes on, but
in the following chapter you&#8217;ll learn how to do the detailed low-level
work by making decisions within a method. For this chapter, sending messages
will suffice.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I60' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I61>
</FONT><A NAME="_Toc375545229"></A><A NAME="_Toc481064522"></A><BR></P></DIV>
<A NAME="Heading100"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Building a Java program</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are several other issues you must
understand before seeing your first Java program.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I61' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I62>
</FONT><A NAME="_Toc375545230"></A><A NAME="_Toc481064523"></A><BR></P></DIV>
<A NAME="Heading101"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Name visibility</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A problem in any programming language is
the control of names. If you use a name in one module of the program, and
another programmer uses the same name in another module, how do you distinguish
one name from another and prevent the two names from &#8220;clashing?&#8221; In
C this is a particular problem because a program is often an unmanageable sea of
names. C++ classes (on which Java classes are based) nest functions within
classes so they cannot clash with function names nested within other classes.
However, C++ still allowed global data and global functions, so clashing was
still possible. To solve this problem, C++ introduced <I>namespaces</I> using
additional keywords. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I62' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I63>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java was able to avoid all of this by
taking a fresh approach. To produce an unambiguous name for a library, the
specifier used is not unlike an Internet domain name. In fact, the Java creators
want you to use your Internet domain name in reverse since those are guaranteed
to be unique. Since my domain name is <B>BruceEckel.com</B>, my utility library
of foibles would be named <B>com.bruceeckel.utility.foibles</B>. After your
reversed domain name, the dots are intended to represent subdirectories.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I63' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I64>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java 1.0 and Java 1.1 the domain
extensions <B>com</B>, <B>edu</B>, <B>org</B>, <B>net</B>, etc., were
<A NAME="Index188"></A><A NAME="Index189"></A>capitalized by convention, so the
library would appear: <B>COM.bruceeckel.utility.foibles</B>. Partway through the
development of Java 2, however, it was discovered that this caused problems, and
so now the entire package name is lowercase.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I64' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I65>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This mechanism means that all of your
files automatically live in their own namespaces, and each class within a file
must have a unique identifier. So you do not need to learn special language
features to solve this problem&#8212;the language takes care of it for you.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I65' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I66>
</FONT><A NAME="_Toc375545231"></A><A NAME="_Toc481064524"></A><BR></P></DIV>
<A NAME="Heading102"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Using other components</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Whenever you want to use a predefined
class in your program, the compiler must know how to locate it. Of course, the
class might already exist in the same source code file that it&#8217;s being
called from. In that case, you simply use the class&#8212;even if the class
doesn&#8217;t get defined until later in the file. Java eliminates the
&#8220;forward referencing&#8221; problem so you don&#8217;t need to think about
it. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I66' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I67>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What about a class that exists in some
other file? You might think that the compiler should be smart enough to simply
go and find it, but there is a problem. Imagine that you want to use a class of
a particular name, but more than one definition for that class exists
(presumably these are different definitions). Or worse, imagine that
you&#8217;re writing a program, and as you&#8217;re building it you add a new
class to your library that conflicts with the name of an existing class.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I67' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I68>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To solve this problem, you must eliminate
all potential ambiguities. This is accomplished by telling the Java compiler
exactly what classes you want using the <B>import</B> keyword. <B>import
</B>tells the compiler to bring in a <I>package</I>, which is a library of
classes. (In other languages, a library could consist of functions and data as
well as classes, but remember that all code in Java must be written inside a
class.)  
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I68' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I69>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most of the time you&#8217;ll be using
components from the standard Java libraries that come with your compiler. With
these, you don&#8217;t need to worry about long, reversed domain names; you just
say, for example:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> java.util.ArrayList;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">to tell the compiler that you want to use
Java&#8217;s <B>ArrayList</B> class. However, <B>util</B> contains a number of
classes and you might want to use several of them without declaring them all
explicitly. This is easily accomplished by using &#8216;<B>*</B>&#8217; to
indicate a wild card:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>import</font> java.util.*;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is more common to import a collection
of classes in this manner than to import classes individually.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I69' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I70>
</FONT><A NAME="_Toc375545232"></A><A NAME="_Toc481064525"></A><BR></P></DIV>
<A NAME="Heading103"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The static keyword</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Ordinarily, when you create a class you
are describing how objects of that class look and how they will behave. You
don&#8217;t actually get anything until you create an object of that class with
<B>new</B>, and at that point data storage is created and methods become
available. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I70' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I71>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But there are two situations in which
this approach is not sufficient. One is if you want to have only one piece of
storage for a particular piece of data, regardless of how many objects are
created, or even if no objects are created. The other is if you need a method
that isn&#8217;t associated with any particular object of this class. That is,
you need a method that you can call even if no objects are created. You can
achieve both of these effects with the <B>static</B> keyword. When you say
something is <B>static</B>, it means that data or method is not tied to any
particular object instance of that class. So even if you&#8217;ve never created
an object of that class you can call a <B>static</B> method or access a piece of
<B>static</B> data. With ordinary, non-<B>static</B> data and methods you must
create an object and use that object to access the data or method, since
non-<B>static</B> data and methods must know the particular object they are
working with. Of course, since <B>static</B> methods don&#8217;t need any
objects to be created before they are used, they cannot <I>directly </I>access
non-<B>static</B> members or methods by simply calling those other members
without referring to a named object (since non-<B>static</B> members and methods
must be tied to a particular object).

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I71' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I72>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Some object-oriented languages use the
terms <I>class data</I> and <I>class methods</I>, meaning that the data and
methods exist only for the class as a whole, and not for any particular objects
of the class. Sometimes the Java literature uses these terms too.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I72' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I73>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To make a data member or method
<B>static</B>, you simply place the keyword before the definition. For example,
the following produces a <B>static</B> data member and initializes it:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I73' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I74>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> StaticTest {
    <font color=#0000ff>static</font> <font color=#0000ff>int</font> i = 47;
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now even if you make two
<B>StaticTest</B> objects, there will still be only one piece of storage for
<B>Stat

⌨️ 快捷键说明

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