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

📄 chap02.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:

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

<backtalk:display ID=TIJ3_CHAPTER2_I24>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>BigInteger</B> supports
arbitrary-precision integers. This means that you can accurately represent
integral values of any size without losing any information during operations.

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

<backtalk:display ID=TIJ3_CHAPTER2_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>BigDecimal</B> is for
arbitrary-precision fixed-point numbers; you can use these for accurate monetary
calculations, for example. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I25' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consult your online documentation for
details about the constructors and methods you can call for these two classes.

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

<backtalk:display ID=TIJ3_CHAPTER2_I27>
</FONT><A NAME="_Toc375545221"></A><A NAME="_Toc481064514"></A><BR></P></DIV>
<A NAME="Heading91"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Arrays in Java</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Virtually all programming languages
support arrays. Using arrays in C and C++ is perilous because those arrays are
only blocks of memory. If a program accesses the array outside of its memory
block or uses the memory before initialization (common programming errors) there
will be unpredictable results.

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

<backtalk:display ID=TIJ3_CHAPTER2_I28>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One of the primary goals of Java is
safety, so many of the problems that plague programmers in C and C++ are not
repeated in Java. A Java array is guaranteed to be initialized and cannot be
accessed outside of its range. The range checking comes at the price of having a
small amount of memory overhead on each array as well as verifying the index at
run-time, but the assumption is that the safety and increased productivity is
worth the expense. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I28' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I29>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you create an array of objects, you
are really creating an array of references, and each of those references is
automatically initialized to a special value with its own keyword:
<A NAME="Index186"></A><A NAME="Index187"></A><B>null</B>. When Java sees
<B>null</B>, it recognizes that the reference in question isn&#8217;t pointing
to an object.<B> </B>You must assign an object to each reference before you use
it, and if you try to use a reference that&#8217;s still <B>null,</B> the
problem will be reported at run-time. Thus, typical array errors are prevented
in Java. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I29' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I30>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can also create an array of
primitives. Again, the compiler guarantees initialization because it zeroes the
memory for that array. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I30' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I31>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Arrays will be covered in detail in later
chapters.

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

<backtalk:display ID=TIJ3_CHAPTER2_I32>
</FONT><A NAME="_Toc375545222"></A><A NAME="_Toc481064515"></A><BR></P></DIV>
<A NAME="Heading92"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
You never need to <BR>destroy an object</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In most programming languages, the
concept of the lifetime of a variable occupies a significant portion of the
programming effort. How long does the variable last? If you are supposed to
destroy it, when should you? Confusion over variable lifetimes can lead to a lot
of bugs, and this section shows how Java greatly simplifies the issue by doing
all the cleanup work for you.

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

<backtalk:display ID=TIJ3_CHAPTER2_I33>
</FONT><A NAME="_Toc375545223"></A><A NAME="_Toc481064516"></A><BR></P></DIV>
<A NAME="Heading93"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Scoping</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most procedural languages have the
concept of <I>scope</I>. This determines both the visibility and lifetime of the
names defined within that scope. In C, C++, and Java, scope is determined by the
placement of curly braces <B>{}</B>. So for example:

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

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

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
  <font color=#0000ff>int</font> x = 12;
  <font color=#009900>/* only x available */</font>
  {
    <font color=#0000ff>int</font> q = 96;
    <font color=#009900>/* both x &amp; q available */</font>
  }
  <font color=#009900>/* only x available */</font>
  <font color=#009900>/* q &#8220;out of scope&#8221; */</font>
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A variable defined within a scope is
available only to the end of that scope.

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

<backtalk:display ID=TIJ3_CHAPTER2_I35>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Indentation makes Java code easier to
read. Since Java is a free-form language, the extra spaces, tabs, and carriage
returns do not affect the resulting program.

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

<backtalk:display ID=TIJ3_CHAPTER2_I36>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that you <I>cannot</I> do the
following, even though it is legal in C and C++:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
  <font color=#0000ff>int</font> x = 12;
  {
    <font color=#0000ff>int</font> x = 96; <font color=#009900>/* illegal */</font>
  }
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The compiler will announce that the
variable <B>x </B>has already been defined. Thus the C and C++ ability to
&#8220;hide&#8221; a variable in a larger scope is not allowed because the Java
designers thought that it led to confusing programs.

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

<backtalk:display ID=TIJ3_CHAPTER2_I37>
</FONT><A NAME="_Toc375545224"></A><A NAME="_Toc481064517"></A><BR></P></DIV>
<A NAME="Heading94"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Scope of objects</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java objects do not have the same
lifetimes as primitives. When you create a Java object using <B>new</B>, it
hangs around past the end of the scope. Thus if you use:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
  String s = <font color=#0000ff>new</font> String(<font color=#004488>"a string"</font>);
} <font color=#009900>/* end of scope */</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">the reference <B>s</B> vanishes at the
end of the scope. However, the <B>String</B> object that <B>s</B> was pointing
to is still occupying memory. In this bit of code, there is no way to access the
object because the only reference to it is out of scope. In later chapters
you&#8217;ll see how the reference to the object can be passed around and
duplicated during the course of a program.

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

<backtalk:display ID=TIJ3_CHAPTER2_I38>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It turns out that because objects created
with <B>new</B> stay around for as long as you want them, a whole slew of C++
programming problems simply vanish in Java. The hardest problems seem to occur
in C++ because you don&#8217;t get any help from the language in making sure
that the objects are available when they&#8217;re needed. And more important, in
C++ you must make sure that you destroy the objects when you&#8217;re done with
them. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I38' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I39>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">That brings up an interesting question.
If Java leaves the objects lying around, what keeps them from filling up memory
and halting your program? This is exactly the kind of problem that would occur
in C++. This is where a bit of magic happens. Java has a <I>garbage
collector</I>, which looks at all the objects that were created with <B>new</B>
and figures out which ones are not being referenced anymore. Then it releases
the memory for those objects, so the memory can be used for new objects. This
means that you never need to worry about reclaiming memory yourself. You simply
create objects, and when you no longer need them they will go away by
themselves. This eliminates a certain class of programming problem: the
so-called &#8220;memory leak,&#8221; in which a programmer forgets to release
memory.

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

<backtalk:display ID=TIJ3_CHAPTER2_I40>
</FONT><A NAME="_Toc375545225"></A><A NAME="_Toc481064518"></A><BR></P></DIV>
<A NAME="Heading95"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Creating new <BR>data types: class</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If everything is an object, what
determines how a particular class of object looks and behaves? Put another way,
what establishes the <I>type</I> of an object? You might expect there to be a
keyword called &#8220;type,&#8221; and that certainly would have made sense.
Historically, however, most object-oriented languages have used the keyword
<B>class</B> to mean &#8220;I&#8217;m about to tell you what a new type of
object looks like.&#8221; The <B>class</B> keyword (which is so common that it
will not be emboldened throughout this book) is followed by the name of the new
type. For example: 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I40' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

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

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> ATypeName { <font color=#009900>/* class body goes here */</font> }</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This introduces a new type, so you can
now create an object of this type using <B>new</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>ATypeName a = <font color=#0000ff>new</font> ATypeName();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>ATypeName</B>, the class body
consists only of a comment (the stars and slashes and what is inside, which will
be discussed later in this chapter), so there is not too much that you can do
with it. In fact, you cannot tell it to do much of anything (that is, you cannot
send it any interesting messages) until you define some methods for it.

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

<backtalk:display ID=TIJ3_CHAPTER2_I42>
</FONT><A NAME="_Toc375545226"></A><A NAME="_Toc481064519"></A><BR></P></DIV>
<A NAME="Heading96"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Fields and methods</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you define a class (and all you do
in Java is define classes, make objects of those classes, and send messages to
those objects), you can put two types of elements in your class: data members
(sometimes called <I>fields</I>), and member functions (typically called
<I>methods</I>). A data member is an object of any type that you can communicate
with via its reference. It can also be one of the primitive types (which
isn&#8217;t a reference). If it is a reference to an object, you must initialize
that reference to connect it to an actual object (using <B>new</B>, as seen
earlier) in a special function called a <I>constructor</I> (described fully in
Chapter 4). If it is a primitive type you can initialize it directly at the
point of definition in the class. (As you&#8217;ll see later, references can
also be initialized at the point of definition.)

⌨️ 快捷键说明

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