📄 chapter02.html
字号:
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">–</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Boolean</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">char</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">16-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Unicode 0</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Unicode 2<SUP>16</SUP>-
1</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Character</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">byte </FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">8-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">-128</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">+127</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Byte</B></FONT><A NAME="fnB11" HREF="#fn11">[11]</A><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">short</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">16-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">-2<SUP>15</SUP></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">+2<SUP>15</SUP> –
1</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Short<SUP>1</SUP></B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">int</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">32-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">-2<SUP>31</SUP></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">+2<SUP>31</SUP> –
1</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Integer</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">long</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">64-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">-2<SUP>63</SUP></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">+2<SUP>63</SUP> –
1</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Long</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">float</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">32-bit</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">IEEE754</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">IEEE754</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Float</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">double</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">64-bit </FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">IEEE754</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">IEEE754</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Double</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">void</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">–</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">–</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">–</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Void</B><SUP>1</SUP></FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All numeric types are signed, so
don’t go looking for unsigned types.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The primitive data types also have
“wrapper”<I> </I>classes for them. That means that if you want to
make a non-primitive object on the heap to represent that primitive type, you
use the associated wrapper. For example:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>char</font> c = 'x';
Character C = <font color=#0000ff>new</font> Character(c);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">or you could also
use:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Character C = <font color=#0000ff>new</font> Character('x');</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The reasons for doing this will be
shown in a later chapter.</FONT><BR></P></DIV>
<A NAME="Heading65"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
High-precision numbers</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java 1.1<A NAME="Index73"></A> has
added two classes for performing high-precision arithmetic: <B>BigInteger</B>
and <B>BigDecimal</B>. Although these approximately fit into the same category
as the “wrapper” classes, neither one has a primitive
analogue.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Both classes have methods that
provide analogues for the operations that you perform on primitive types. That
is, you can do anything with a <B>BigInteger</B> or <B>BigDecimal </B>that you
can with an <B>int</B> or <B>float</B>, it’s just that you must use method
calls instead of operators. Also, since there’s more involved, the
operations will be slower. You’re exchanging speed for
accuracy.</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.</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.</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.</FONT><A NAME="_Toc375545221"></A><A NAME="_Toc408018422"></A><BR></P></DIV>
<A NAME="Heading66"></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.</FONT><A NAME="fnB12" HREF="#fn12">[12]</A><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.</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 handles, and each of those handles
is automatically initialized to a special value with its own keyword:
<A NAME="Index74"></A><A NAME="Index75"></A><B>null</B>. When Java sees
<B>null</B>, it recognizes that the handle in question isn’t pointing to
an object.<B> </B>You must assign an object to each handle before you use it,
and if you try to use a handle that’s still <B>null,</B> the problem will
be reported at run-time. Thus, typical array errors are prevented in
Java.</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.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Arrays will be covered in detail in
later
chapters.</FONT><A NAME="_Toc375545222"></A><A NAME="_Toc408018423"></A><BR></P></DIV>
<A NAME="Heading67"></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.</FONT><A NAME="_Toc375545223"></A><A NAME="_Toc408018424"></A><BR></P></DIV>
<A NAME="Heading68"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Scoping</H3></FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -