📄 tij0033.html
字号:
<html><body>
<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0032.html">Prev</a> | <a href="tij0034.html">Next</a>
</td>
</tr></table>
<hr>
<H2 ALIGN=LEFT>
You
must create
<P>all
the objects
</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">When
you create a handle, you want to connect it with a new object. You do so, in
general, with the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword.
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
says, “Make me a new one of these objects.” So in the above
example, you can say:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">String
s = new String("asdf");
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Not
only does this mean “Make me a new
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,”
but it also gives information about
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>how</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to make the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by supplying an initial character string.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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><a name="_Toc375545219"></a><a name="_Toc408018420"></a><P></DIV>
<A NAME="Heading64"></A><H3 ALIGN=LEFT>
Where
storage lives
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B> Registers</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is the fastest storage because it exists in a place different than 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><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B> The
stack
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This lives in the general RAM (random-access memory) area, but has direct
support from the processor via its
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>stack
pointer
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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 handles
– Java objects are not placed on the stack.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B> The
heap
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">and
the storage is allocated on the heap when that code is executed. And of course
there’s a price you pay for this flexibility: it takes more time to
allocate heap storage.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B> Static
storage
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
“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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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="Carmina Md BT" SIZE=3 COLOR="Black"><B> Constant
storage
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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="Carmina Md BT" SIZE=3 COLOR="Black"><B> Non-RAM
storage
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>streamed
objects,
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in which objects are turned into streams of bytes, generally to be sent to
another machine, and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>persistent
objects,
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>lightweight
persistence
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and future versions of Java might provide more complete solutions for
persistence.
</FONT><a name="_Toc375545220"></a><a name="_Toc408018421"></a></OL><A NAME="Heading65"></A><H3 ALIGN=LEFT>
Special
case: primitive types
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
is a group of types that gets special treatment; you can think of these as
“primitive” types that you use quite often in your programming. The
reason for the special treatment is that to create an object with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
especially a small, simple variable, isn’t very efficient because
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
an “automatic” variable is created that
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>is
not a handle
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The variable holds the value, and it’s placed on the stack so it’s
much more efficient.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 so portable.
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
<COL width="76">
<COL width="45">
<COL width="67">
<COL width="77">
<COL width="64">
</COLGROUP>
<TR VALIGN="TOP">
<TH WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Primitive
type
</FONT><P></DIV>
</TH>
<TH WIDTH=45 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Size</FONT><P></DIV>
</TH>
<TH WIDTH=67 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Minimum</FONT><P></DIV>
</TH>
<TH WIDTH=77 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Maximum</FONT><P></DIV>
</TH>
<TH WIDTH=64 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Wrapper
type
</FONT><P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">boolean</FONT><P></DIV>
</TD>
<TD WIDTH=45 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">1-bit</FONT><P></DIV>
</TD>
<TD WIDTH=67 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–</FONT><P></DIV>
</TD>
<TD WIDTH=77 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">–</FONT><P></DIV>
</TD>
<TD WIDTH=64 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Boolean</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">char</FONT><P></DIV>
</TD>
<TD WIDTH=45 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">16-bit</FONT><P></DIV>
</TD>
<TD WIDTH=67 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Unicode
0
</FONT><P></DIV>
</TD>
<TD WIDTH=77 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Unicode
2
</FONT><SUP><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">16</FONT></SUP><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">-
1
</FONT><P></DIV>
</TD>
<TD WIDTH=64 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Character</B></FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=76 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">byte
</FONT><P></DIV>
</TD>
<TD WIDTH=45 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -