📄 tij304.htm
字号:
<td width="59.999985" colspan="1" rowspan="1" valign="top">
<p class="Table">32-bit<br></p>
</td>
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table">IEEE754<br></p>
</td>
<td width="113.333305" colspan="1" rowspan="1" valign="top">
<p class="Table">IEEE754<br></p>
</td>
<td width="103.999974" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Float</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>double</b><br></p>
</td>
<td width="59.999985" colspan="1" rowspan="1" valign="top">
<p class="Table">64-bit <br></p>
</td>
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table">IEEE754<br></p>
</td>
<td width="113.333305" colspan="1" rowspan="1" valign="top">
<p class="Table">IEEE754<br></p>
</td>
<td width="103.999974" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Double</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>void</b><br></p>
</td>
<td width="59.999985" colspan="1" rowspan="1" valign="top">
<p class="Table">—<br></p>
</td>
<td width="95.999976" colspan="1" rowspan="1" valign="top">
<p class="Table">—<br></p>
</td>
<td width="113.333305" colspan="1" rowspan="1" valign="top">
<p class="Table">—<br></p>
</td>
<td width="103.999974" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Void</b><br></p>
</td>
</tr>
</table></div>
<p class="TableFollower">All numeric types are signed, so don’t look for unsigned types. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_376" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The size of the <b>boolean</b> type is not explicitly specified; it is only defined to be able to take the literal values <b>true</b> or <b>false</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0107" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The “wrapper”<i> </i>classes for the primitive data types allow you to make a nonprimitive object on the heap to represent that primitive type. For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_377" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>char</font> c = 'x';
Character C = <font color=#0000ff>new</font> Character(c);</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Or you could also use:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Character C = <font color=#0000ff>new</font> Character('x');</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The reasons for doing this will be shown in a later chapter. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_378" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1334"></a>High-precision numbers</h4>
<p>Java includes 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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_379" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_380" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_381" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><b>BigDecimal</b> is for arbitrary-precision fixed-point numbers; you can use these for accurate monetary calculations, for example. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_382" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Consult the JDK documentation for details about the constructors and methods you can call for these two classes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_383" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545221"></a><a name="_Toc24775547"></a><a name="Heading1340"></a>Arrays
in Java</h3>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_384" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_385" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>When you create an array of objects, you are really creating an array of references, and each of those references is automatically initialized to a special value with its own keyword: <a name="Index119"></a><a name="Index120"></a><b>null</b>. When Java sees <b>null</b>, it recognizes that the reference in question isn’t pointing to an object.<b> </b>You must assign an object to each reference before you use it, and if you try to use a reference that’s still <b>null,</b> the problem will be reported at run time. Thus, typical array errors are prevented in Java. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_386" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>You can also create an array of primitives. Again, the compiler guarantees initialization because it zeroes the memory for that array. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_387" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Arrays will be covered in detail in later chapters. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_388" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545222"></a><a name="_Toc24775548"></a><a name="Heading1346"></a>You
never need to <br>destroy an object</h2>
<p>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 size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_389" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545223"></a><a name="_Toc24775549"></a><a name="Heading1348"></a>Scoping</h3>
<p>Most procedural languages have the concept of <i>scope</i>. This determines both the visibility and lifetime of the names defined within that scope. In C, C++, and Java, scope is determined by the placement of curly braces <b>{}</b>. So for example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_390" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
<font color=#0000ff>int</font> x = 12;
<font color=#009900>// Only x available</font>
{
<font color=#0000ff>int</font> q = 96;
<font color=#009900>// Both x & q available</font>
}
<font color=#009900>// Only x available</font>
<font color=#009900>// q “out of scope”</font>
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>A variable defined within a scope is available only to the end of that scope. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_391" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Any text after a ‘//’ to the end of a line is a comment. <br></p>
<p>Indentation makes Java code easier to read. Since Java is a free-form language, the extra spaces, tabs, and carriage returns do not affect the resulting program. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_392" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Note that you <i>cannot</i> do the following, even though it is legal in C and C++:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
<font color=#0000ff>int</font> x = 12;
{
<font color=#0000ff>int</font> x = 96; <font color=#009900>// Illegal</font>
}
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The compiler will announce that the variable <b>x </b>has already been defined. Thus the C and C++ ability to “hide” a variable in a larger scope is not allowed, because the Java designers thought that it led to confusing programs. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_393" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545224"></a><a name="_Toc24775550"></a><a name="Heading1373"></a>Scope
of objects</h3>
<p>Java objects do not have the same lifetimes as primitives. When you create a Java object using <b>new</b>, it hangs around past the end of the scope. Thus if you use:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>{
String s = <font color=#0000ff>new</font> String(<font color=#004488>"a string"</font>);
} <font color=#009900>// End of scope</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>the reference <b>s</b> vanishes at the end of the scope. However, the <b>String</b> object that <b>s</b> was pointing to is still occupying memory. In this bit of code, there is no way to access the object, because the only reference to it is out of scope. In later chapters you’ll see how the reference to the object can be passed around and duplicated during the course of a program. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_394" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>It turns out that because objects created with <b>new</b> stay around for as long as you want them, a whole slew of C++ programming problems simply vanish in Java. The hardest problems seem to occur in C++ because you don’t get any help from the language in making sure that the objects are available when they’re needed. And more important, in C++ you must make sure that you destroy the objects when you’re done with them. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_395" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>That brings up an interesting question. If Java leaves the objects lying around, what keeps them from filling up memory and halting your program? This is exactly the kind of problem that would occur in C++. This is where a bit of magic happens. Java has a <i>garbage collector</i>, which looks at all the objects that were created with <b>new</b> and figures out which ones are not being referenced anymore. Then it releases the memory for those objects, so the memory can be used for new objects. This means that you never need to worry about reclaiming memory yourself. You simply create objects, and when you no longer need them, they will go away by themselves. This eliminates a certain class of programming problem: the so-called “memory leak,” in which a programmer forgets to release memory. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_396" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545225"></a><a name="_Toc24775551"></a><a name="Heading1382"></a>Creating
new <br>data types: class</h2>
<p>If everything is an object, what determines how a particular class of object looks and behaves? Put another way, what establishes the <i>type</i> of an object? You might expect there to be a keyword called “type,” and that certainly would have made sense. Historically, however, most object-oriented languages have used the keyword <b>class</b> to mean “I’m about to tell you what a new type of object looks like.” The <b>class</b> keyword (which is so common that it will not be bold-faced throughout this book) is followed by the name of the new type. For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_397" title="Send BackTalk Comment">Feedback</a></font><br></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -