📄 chap02.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:04
Translation Platform:Win32
Number of Output files:23
This File:Chap02.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>2: Everything is an Object</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README.txt">Viewing Hints</a> ]
[ <a href="RevHist.htm">Revision History</a> ]
[ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
[ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
[ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
[ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
[ <a href="http://www.mindview.net/Services">Consulting</a> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 2nd edition, Revision 12</FONT></H2>
<H3><FONT FACE="Verdana">©2000 by Bruce Eckel</FONT></H3>
<FONT FACE="Verdana" size = "-1">
[ <a href="Chap01.htm">Previous Chapter</a> ]
[ <a href="SimpCont.htm">Short TOC</a> ]
[ <a href="Contents.htm">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Chap03.htm">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
2: Everything<BR>is an Object</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER2_I0>
Although
it is based on C++, Java is more of a “pure” object-oriented
language.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Both C++ and Java are hybrid languages,
but in Java the designers felt that the hybridization was not as important as it
was in C++. A hybrid language allows multiple programming styles; the reason C++
is hybrid is to support backward compatibility with the C language. Because C++
is a superset of the C language, it includes many of that language’s
undesirable features, which can make some aspects of C++ overly complicated.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I0'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java language assumes that you want
to do only object-oriented programming. This means that before you can begin you
must shift your mindset into an object-oriented world (unless it’s already
there)<I>.</I> The benefit of this initial effort is the ability to program in a
language that is simpler to learn and to use than many other OOP languages. In
this chapter we’ll see the basic components of a Java program and
we’ll learn that everything in Java is an object, even a Java program.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I1'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I2>
</FONT><A NAME="_Toc375545217"></A><A NAME="_Toc481064510"></A><BR></P></DIV>
<A NAME="Heading86"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
You manipulate objects <BR>with references </H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each programming language has its own
means of manipulating data. Sometimes the programmer must be constantly aware of
what type of manipulation is going on. Are you manipulating the object directly,
or are you dealing with some kind of indirect representation (a pointer in C or
C++) that must be treated with a special syntax?
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I2'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All this is simplified in Java. You treat
everything as an object, so there is a single consistent syntax that you use
everywhere. Although you <I>treat </I>everything as an object, the identifier
you manipulate is actually a “reference” to an
object</FONT><A NAME="fnB20" HREF="#fn20">[20]</A><FONT FACE="Georgia">. You
might imagine this scene as a television (the object) with your remote control
(the reference). As long as you’re holding this reference, you have a
connection to the television, but when someone says “change the
channel” or “lower the volume,” what you’re manipulating
is the reference, which in turn modifies the object. If you want to move around
the room and still control the television, you take the remote/reference with
you, not the television.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I3'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Also, the remote control can stand on its
own, with no television. That is, just because you have a reference
doesn’t mean there’s necessarily an object connected to it. So if
you want to hold a word or sentence, you create a <B>String</B> reference:<I>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I4'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I5>
</I></FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But here you’ve created <I>only</I>
the reference, not an object. If you decided to send a message to <B>s</B> at
this point, you’ll get an error (at run-time) because <B>s</B> isn’t
actually attached to anything (there’s no television). A safer practice,
then, is always to initialize a reference when you create it:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I5'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I6>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s = <font color=#004488>"asdf"</font>;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, this uses a special Java
feature: strings can be initialized with quoted text. Normally, you must use a
more general type of initialization for objects.<I>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I6'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I7>
</I></FONT><A NAME="_Toc375545218"></A><A NAME="_Toc481064511"></A><BR></P></DIV>
<A NAME="Heading87"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
You must create <BR>all the objects</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you create a reference, you want to
connect it with a new object. You do so, in general, with the <B>new</B>
keyword. <B>new</B> says, “Make me a new one of these objects.” So
in the above example, you can say:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I7'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I8>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s = <font color=#0000ff>new</font> String(<font color=#004488>"asdf"</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Not only does this mean “Make me a
new <B>String</B>,” but it also gives information about <I>how</I> to make
the <B>String</B> by supplying an initial character string.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I8'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I9>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Of course, <B>String</B> is not the only
type that exists. Java comes with a plethora of ready-made types. What’s
more important is that you can create your own types. In fact, that’s the
fundamental activity in Java programming, and it’s what you’ll be
learning about in the rest of this book.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I9'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I10>
</FONT><A NAME="_Toc375545219"></A><A NAME="_Toc481064512"></A><BR></P></DIV>
<A NAME="Heading88"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Where storage lives</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It’s useful to visualize some
aspects of how things are laid out while the program is running, in particular
how memory is arranged. There are six different places to store data:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I10'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I11>
</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Registers</B>.
This is the fastest storage because it exists in a place different from that of
other storage: inside the processor. However, the number of registers is
severely limited, so registers are allocated by the compiler according to its
needs. You don’t have direct control, nor do you see any evidence in your
programs that registers even exist.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I11'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I12>
</FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>The
stack</B>. This lives in the general RAM (random-access memory) area, but has
direct support from the processor via its <I>stack pointer</I>. The stack
pointer is moved down to create new memory and moved up to release that memory.
This is an extremely fast and efficient way to allocate storage, second only to
registers. The Java compiler must know, while it is creating the program, the
exact size and lifetime of all the data that is stored on the stack, because it
must generate the code to move the stack pointer up and down. This constraint
places limits on the flexibility of your programs, so while some Java storage
exists on the stack—in particular, object references—Java objects
themselves are not placed on the stack.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I13>
</FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>The
heap</B>. This is a general-purpose pool of memory (also in the RAM area) where
all Java objects live. The nice thing about the heap is that, unlike the stack,
the compiler doesn’t need to know how much storage it needs to allocate
from the heap or how long that storage must stay on the heap. Thus,
there’s a great deal of flexibility in using storage on the heap. Whenever
you need to create an object, you simply write the code to create it using
<B>new</B>,<B> </B>and the storage is allocated on the heap when that code is
executed. Of course there’s a price you pay for this flexibility: it takes
more time to allocate heap storage than it does to allocate stack storage (that
is, if you even <I>could</I> create objects on the stack in Java, as you can in
C++).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I14>
</FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Static
storage</B>. “Static” is used here in the sense of “in a fixed
location” (although it’s also in RAM). Static storage contains data
that is available for the entire time a program is running. You can use the
<B>static</B> keyword to specify that a particular element of an object is
static, but Java objects themselves are never placed in static storage.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER2_I15>
</FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Constant
storage</B>. Constant values are often placed directly in the program code,
which is safe since they can never change. Sometimes constants are cordoned off
by themselves so that they can be optionally placed in read-only memory (ROM).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I15'
target="_blank">Add Comment</a> ]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -