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

📄 chapter02.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:24:36
Translation Platform:Win32
Number of Output files:27
This File:Chapter02.html
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-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
    [ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 1st edition</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;1998 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter01.html">Previous Chapter</a> ] 
    [ <a href="SimpleContents.html">Short TOC</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter03.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="OLE_LINK5"></A><A NAME="Chapter_2"></A><A NAME="_Toc375545216"></A><A NAME="_Toc407441446"></A><A NAME="_Toc408018417"></A><A NAME="Heading60"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
2: Everything<BR>is an object</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>Although it is based on
C++, Java is more of a &#8220;pure&#8221; 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&#8217;s undesirable features which can make some aspects of C++ overly
complicated.</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&#8217;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&#8217;ll see the basic components of a
Java program and we&#8217;ll learn that everything in Java is an object, even a
Java
program.</FONT><A NAME="_Toc375545217"></A><A NAME="_Toc408018418"></A><BR></P></DIV>
<A NAME="Heading61"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
You manipulate objects <BR>with handles </H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each programming language has its
own means of manipulating data. Sometimes the programmer must constantly be
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><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 &#8220;handle&#8221; to an object. (You
might see this called a <I>reference</I> or even a pointer in other discussions
of Java.) You might imagine this scene as a television (the object) with your
remote control (the handle). As long as you&#8217;re holding this handle, you
have a connection to the television, but when someone says &#8220;change the
channel&#8221; or &#8220;lower the volume,&#8221; what you&#8217;re manipulating
is the handle, which in turn modifies the object. If you want to move around the
room and still control the television, you take the remote/handle with you, not
the television.</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 handle
doesn&#8217;t mean there&#8217;s necessarily an object connected to it. So if
you want to hold a word or sentence, you create a <B>String</B>
handle:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>String s;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But here you&#8217;ve created
<I>only</I> the handle, not an object. If you decided to send a message to
<B>s</B> at this point, you&#8217;ll get an error (at run-time) because <B>s</B>
isn&#8217;t actually attached to anything (there&#8217;s no television). A safer
practice, then, is always to initialize a handle when you create
it:</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 case:
strings can be initialized with quoted text. Normally, you must use a more
general type of initialization for
objects.</FONT><A NAME="_Toc375545218"></A><A NAME="_Toc408018419"></A><BR></P></DIV>
<A NAME="Heading62"></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 handle, you want
to connect it with a new object. You do so, in general, with the <B>new</B>
keyword. <B>new</B> says, &#8220;Make me a new one of these objects.&#8221; So
in the above example, you can say:</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 &#8220;Make
me a new <B>String</B>,&#8221; but it also gives information about <I>how</I> to
make the <B>String</B> by supplying an initial character
string.</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&#8217;s more important is that you can create your own types. In fact,
that&#8217;s the fundamental activity in Java programming, and it&#8217;s what
you&#8217;ll be learning about in the rest of this
book.</FONT><A NAME="_Toc375545219"></A><A NAME="_Toc408018420"></A><BR></P></DIV>
<A NAME="Heading63"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Where storage lives</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It&#8217;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><BR></P></DIV>
<OL>
<LI><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&#8217;t have
direct control, nor do you see any evidence in your programs that registers even
exist.</FONT><LI><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
&#8211; in particular, object handles &#8211; Java objects are not placed on the
stack.</FONT><LI><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&#8217;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&#8217;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>and the storage
is allocated on the heap when that code is executed. And of course there&#8217;s
a price you pay for this flexibility: it takes more time to allocate heap
storage.</FONT><LI><FONT FACE="Georgia"><B>	Static storage</B>.
&#8220;Static&#8221; is used here in the sense of &#8220;in a fixed
location&#8221; (although it&#8217;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><LI><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).</FONT><LI><FONT FACE="Georgia"><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
1.1<A NAME="Index72"></A> provides support for <I>lightweight persistence</I>,
and future versions of Java might provide more complete solutions for
persistence.</FONT><A NAME="_Toc375545220"></A><A NAME="_Toc408018421"></A></OL><A NAME="Heading64"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Special case: primitive types</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There is a group of types that gets
special treatment; you can think of these as &#8220;primitive&#8221; types that
you use quite often in your programming. The reason for the special treatment is
that to create an object with <B>new</B>, especially a small, simple variable,
isn&#8217;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 using <B>new</B>, an &#8220;automatic&#8221; variable
is created that <I>is not a handle</I>. The variable holds the value, and
it&#8217;s placed on the stack so it&#8217;s much more
efficient.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java determines the size of each
primitive type. These sizes don&#8217;t change from one machine architecture to
another as they do in most languages. This size invariance is one reason Java
programs are so portable.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Primitive type</FONT><BR></P></DIV>
</TH>
<TH WIDTH=45 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Size</FONT><BR></P></DIV>
</TH>
<TH WIDTH=67 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Minimum</FONT><BR></P></DIV>
</TH>
<TH WIDTH=77 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Maximum</FONT><BR></P></DIV>
</TH>
<TH WIDTH=64 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Wrapper type</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">boolean</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">1-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">&#8211;</FONT><BR></P></DIV>

⌨️ 快捷键说明

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