📄 tij304.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<!--
This document was converted from RTF source:
By r2net 5.8 r2netcmd Windows
See http://www.logictran.com
-->
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thinking in Java, 3rd ed. Revision 4.0: 2: Everything is an Object</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"></head>
<body >
<CENTER> <a href="http://www.MindView.net"> <img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a> <Font FACE="Verdana, Tahoma, Arial, Helvetica, Sans"> <h2>Thinking in Java, 3<sup>rd</sup> ed. Revision 4.0</h2> <FONT size = "-1"><br> [ <a href="README.txt">Viewing Hints</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> ] <br><br> </FONT></FONT> </CENTER>
<font face="Georgia"><div align="CENTER"><a href="TIJ303.htm" target="RightFrame"><img src="./prev.gif" alt="Previous " border="0"></a>
<a href="TIJ305.htm" target="RightFrame"><img src="./next.gif" alt="Next " border="0"></a>
<a href="TIJ3_t.htm"><img src="./first.gif" alt="Title Page " border="0"></a>
<a href="TIJ3_i.htm"><img src="./index.gif" alt="Index " border="0"></a>
<a href="TIJ3_c.htm"><img src="./contents.gif" alt="Contents " border="0"></a>
</div>
<hr>
<h1>
<a name="_Toc375545216"></a><a name="_Toc15535411"></a><a name="_Toc24272641"></a><a name="_Toc24775542"></a><a name="Heading1242"></a>2:
Everything<br>is an Object</h1>
<p class="Intro">Although it is based on C++, Java is more of a “pure” object-oriented language.<br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_357" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_358" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545217"></a><a name="_Toc24775543"></a><a name="Heading1246"></a>You
manipulate objects <br>with references </h2>
<p>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? <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_359" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>All this is simplified in Java. You treat everything as an object, using a single consistent syntax. Although you <i>treat </i>everything as an object, the identifier you manipulate is actually a “reference” to an object.<sup><a name="fnB10" href="#fn10">[10]</a></sup> 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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_360" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_361" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_362" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s = <font color=#004488>"asdf"</font>;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_363" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545218"></a><a name="_Toc24775544"></a><a name="Heading1257"></a>You
must create <br>all the objects</h2>
<p>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. The keyword <b>new</b> says, “Make me a new one of these objects.” So in the preceding example, you can say: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_364" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s = <font color=#0000ff>new</font> String(<font color=#004488>"asdf"</font>);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_365" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_366" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545219"></a><a name="_Toc24775545"></a><a name="Heading1263"></a>Where
storage lives</h3>
<p>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: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_367" title="Send BackTalk Comment">Feedback</a></font><br></p>
<ol>
<li><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. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_368" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>The stack</b>. This lives in the general random-access memory (RAM) 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. <font
size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_369"
title="Send BackTalk Comment">Feedback</a></font></li>
<li><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 by 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 (if
you even <i>could</i> create objects on the stack in Java, as you can in C++).
<font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_370"
title="Send BackTalk Comment">Feedback</a></font></li>
<li><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. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_371" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><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), in embedded systems. <font size="-2"><a
href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_372" title="Send BackTalk
Comment">Feedback</a></font></li>
<li><b>Non-RAM storage</b>. If data lives completely outside a program, it can
exist while the program is not running, outside the control of the program. The
two primary examples of this are <i>streamed objects,</i> in which objects are
turned into streams of bytes, generally to be sent to another machine, and
<i>persistent objects, </i>in which the objects are placed on disk so they will
hold their state even when the program is terminated. The trick with these types
of storage is turning the objects into something that can exist on the other
medium, and yet can be resurrected into a regular RAM-based object when
necessary. Java provides support for <i>lightweight persistence</i>, and future
versions of Java might provide more complete solutions for persistence. <font
size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_373"
title="Send BackTalk Comment">Feedback</a></font></li></ol><h3>
<a name="_Toc375545220"></a><a name="_Toc24775546"></a><a name="Heading1271"></a>Special
case: primitive types<br></h3>
<p><a name="Index117"></a><a name="Index118"></a>One group of types, which you’ll use quite often in your programming, gets special treatment. You can think of these as “primitive” types. The reason for the special treatment is that to create an object with <b>new</b>—especially a small, simple variable—isn’t very efficient, because <b>new</b> places objects on the heap. For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable by using <b>new</b>, an “automatic” variable is created that is<i> not a reference</i>. The variable holds the value, and it’s placed on the stack, so it’s much more efficient. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_374" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Java determines the size of each primitive type. These sizes don’t change from one machine architecture to another as they do in most languages. This size invariance is one reason Java programs are portable. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_375" title="Send BackTalk Comment">Feedback</a></font><br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Primitive type</b><br></p>
</th>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -