﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Scott@JAVA-文章分类-Effective Java</title><link>http://www.blogjava.net/cisco/category/6317.html</link><description>Java, 一杯浓浓的咖啡伴你到深夜&lt;br&gt;
&lt;span id="dict_daily"&gt;
&lt;a href="http://dict.cn/" target="_blank"&gt;Dict.CN 在线词典, 英语学习, 在线翻译&lt;/a&gt;
&lt;/span&gt; 
&lt;script language="JavaScript" src="http://dict.cn/daily.php" defer="defer"&gt;
&lt;/script&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 13:00:25 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 13:00:25 GMT</pubDate><ttl>60</ttl><item><title>《Effective Java》Chapter 7</title><link>http://www.blogjava.net/cisco/articles/24448.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 17 Dec 2005 22:26:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/24448.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24448.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/24448.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24448.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24448.html</trackback:ping><description><![CDATA[<STRONG><FONT color=#800080>Chapter 7. General Programming<BR><BR></FONT><FONT color=#ff0000>Item 29: Minimize the scope of local variables</FONT></STRONG><BR><FONT color=#000000>The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.</FONT><BR>Nearly every local variable declaration should contain an initializer.<BR>Keep methods small and focused.<BR><BR>The for loop allows you to declareloop variables, limiting their scope to the exact region where they're needed. (This region consists of the body of the loop as well as the initialization, test, and update preceding the body.) Therefore prefer for loops to while loops.<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><STRONG><FONT color=#800080><IMG id=Codehighlighter1_47_73_Open_Image onclick="this.style.display='none'; Codehighlighter1_47_73_Open_Text.style.display='none'; Codehighlighter1_47_73_Closed_Image.style.display='inline'; Codehighlighter1_47_73_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_47_73_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_47_73_Closed_Text.style.display='none'; Codehighlighter1_47_73_Open_Image.style.display='inline'; Codehighlighter1_47_73_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></FONT></STRONG><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(Iterator&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;c.iterator();&nbsp;i.hasNext();&nbsp;)&nbsp;</SPAN><SPAN id=Codehighlighter1_47_73_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_47_73_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;doSomething(i.next());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_56_75_Open_Image onclick="this.style.display='none'; Codehighlighter1_56_75_Open_Text.style.display='none'; Codehighlighter1_56_75_Closed_Image.style.display='inline'; Codehighlighter1_56_75_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_56_75_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_56_75_Closed_Text.style.display='none'; Codehighlighter1_56_75_Open_Image.style.display='inline'; Codehighlighter1_56_75_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,&nbsp;n&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;expensiveComputation();&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;n;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_56_75_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_56_75_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;doSomething(i);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>Again, this idiom uses two loop variables, and the second variable, n, is used to avoid the cost of performing redundant computation on every iteration. As a rule, you should use this idiom if the loop test involves a method invocation and the method invocation is guaranteed to return the same result on each iteration.<BR><BR><FONT color=#ff0000><STRONG>Item 30: Know and use the libraries</STRONG></FONT><BR><FONT color=#000000>Suppose you want to generate random integers between 0 and some upper bound.</FONT><BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;Random&nbsp;rnd&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Random();<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Common&nbsp;but&nbsp;flawed!</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_82_121_Open_Image onclick="this.style.display='none'; Codehighlighter1_82_121_Open_Text.style.display='none'; Codehighlighter1_82_121_Closed_Image.style.display='inline'; Codehighlighter1_82_121_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_82_121_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_82_121_Closed_Text.style.display='none'; Codehighlighter1_82_121_Open_Image.style.display='inline'; Codehighlighter1_82_121_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;random(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;n)&nbsp;</SPAN><SPAN id=Codehighlighter1_82_121_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_82_121_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;Math.abs(rnd.nextInt())&nbsp;</SPAN><SPAN style="COLOR: #000000">%</SPAN><SPAN style="COLOR: #000000">&nbsp;n;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>The method attempts to map the value returned by rnd.nextInt() into a nonnegative integer with Math.abs. If nextInt() returns Integer.MIN_VALUE, Math.abs will also return Integer.MIN_VALUE, and the remainder operator (%) will return a negative number. Use Random.nextInt(int) instead.<BR><BR>By using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of<BR>those who used it before you.<BR>Numerous features are added to the libraries in every major release, and it pays to keep abreast of these additions.<BR>Every programmer should be familiar with the contents of <EM>java.lang</EM>, <EM>java.util</EM>, and, to a lesser extent, <EM>java.io</EM>.<BR><BR>In the 1.2 release, a <EM>Collections Framework</EM> was added to the <EM>java.util</EM> package. It should be part of every programmer's basic toolkit.<BR><BR>A third-party library worthy of note is Doug Lea's <EM>util.concurrent</EM>, which provides high-level concurrency utilities to simplify the task of multithreaded programming.<BR><BR>There are many additions to the libraries in the 1.4 release. Notable additions include the following:<BR>&nbsp;&nbsp;&nbsp;• <EM>java.util.regex</EM>— A full-blown Perl-like regular expression facility.<BR>&nbsp;&nbsp;&nbsp;• <EM>java.util.prefs</EM>— A facility for the persistent storage of user preferences and program configuration data.<BR>&nbsp;&nbsp;&nbsp;• <EM>java.nio</EM>— A high-performance I/O facility, including scalable I/O (akin to the Unix poll call) and memory-mapped I/O (akin to the Unix mmap call).<BR>&nbsp;&nbsp;&nbsp;• <EM>java.util.LinkedHashSet</EM>, <EM>LinkedHashMap</EM>, <EM>IdentityHashMap</EM>— New collection implementations.<BR><BR><STRONG><FONT color=#ff0000>Item 31: Avoid float and double if exact answers are required</FONT></STRONG><BR><FONT color=#000000>The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.</FONT><BR><BR>For example, suppose you have a dollar in your pocket, and you see a shelf with a row of delicious candies priced at 10, 20, 30, and so forth, up to a dollar.<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Broken&nbsp;-&nbsp;uses&nbsp;floating&nbsp;point&nbsp;for&nbsp;monetary&nbsp;calculation!</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_97_334_Open_Image onclick="this.style.display='none'; Codehighlighter1_97_334_Open_Text.style.display='none'; Codehighlighter1_97_334_Closed_Image.style.display='inline'; Codehighlighter1_97_334_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_97_334_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_97_334_Closed_Text.style.display='none'; Codehighlighter1_97_334_Open_Image.style.display='inline'; Codehighlighter1_97_334_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</SPAN><SPAN id=Codehighlighter1_97_334_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_97_334_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1.00</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_199_237_Open_Image onclick="this.style.display='none'; Codehighlighter1_199_237_Open_Text.style.display='none'; Codehighlighter1_199_237_Closed_Image.style.display='inline'; Codehighlighter1_199_237_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_199_237_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_199_237_Closed_Text.style.display='none'; Codehighlighter1_199_237_Open_Image.style.display='inline'; Codehighlighter1_199_237_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;.</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">;&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;price;&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;.</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_199_237_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_199_237_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">-=</SPAN><SPAN style="COLOR: #000000">&nbsp;price;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemsBought</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;items&nbsp;bought.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Change:&nbsp;$</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;funds);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>If you run the program, you'll find that you can afford three pieces of candy, and you have $0.3999999999999999 left. This is the wrong answer! The right way to solve this problem is to use BigDecimal, int, or long for monetary calculations:<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_39_416_Open_Image onclick="this.style.display='none'; Codehighlighter1_39_416_Open_Text.style.display='none'; Codehighlighter1_39_416_Closed_Image.style.display='inline'; Codehighlighter1_39_416_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_39_416_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_39_416_Closed_Text.style.display='none'; Codehighlighter1_39_416_Open_Image.style.display='inline'; Codehighlighter1_39_416_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</SPAN><SPAN id=Codehighlighter1_39_416_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_39_416_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">final</SPAN><SPAN style="COLOR: #000000">&nbsp;BigDecimal&nbsp;TEN_CENTS&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;BigDecimal(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">.10</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;BigDecimal&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;BigDecimal(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">1.00</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_257_310_Open_Image onclick="this.style.display='none'; Codehighlighter1_257_310_Open_Text.style.display='none'; Codehighlighter1_257_310_Closed_Image.style.display='inline'; Codehighlighter1_257_310_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_257_310_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_257_310_Closed_Text.style.display='none'; Codehighlighter1_257_310_Open_Image.style.display='inline'; Codehighlighter1_257_310_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(BigDecimal&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;TEN_CENTS;&nbsp;funds.compareTo(price)&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;price.add(TEN_CENTS))&nbsp;</SPAN><SPAN id=Codehighlighter1_257_310_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_257_310_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemsBought</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;funds.subtract(price);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;items&nbsp;bought.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Money&nbsp;left&nbsp;over:&nbsp;$</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;funds);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>An alternative to using BigDecimal is to use int (don't exceed nine decimal digits)&nbsp;or long (don't exceed eighteen digits), depending on the amounts involved, and to keep track of the decimal point yourself:<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_39_285_Open_Image onclick="this.style.display='none'; Codehighlighter1_39_285_Open_Text.style.display='none'; Codehighlighter1_39_285_Closed_Image.style.display='inline'; Codehighlighter1_39_285_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_39_285_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_39_285_Closed_Text.style.display='none'; Codehighlighter1_39_285_Open_Image.style.display='inline'; Codehighlighter1_39_285_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</SPAN><SPAN id=Codehighlighter1_39_285_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_39_285_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">100</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_132_170_Open_Image onclick="this.style.display='none'; Codehighlighter1_132_170_Open_Text.style.display='none'; Codehighlighter1_132_170_Closed_Image.style.display='inline'; Codehighlighter1_132_170_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_132_170_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_132_170_Closed_Text.style.display='none'; Codehighlighter1_132_170_Open_Image.style.display='inline'; Codehighlighter1_132_170_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">;&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;=</SPAN><SPAN style="COLOR: #000000">&nbsp;price;&nbsp;price&nbsp;</SPAN><SPAN style="COLOR: #000000">+=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">10</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_132_170_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_132_170_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemsBought</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">-=</SPAN><SPAN style="COLOR: #000000">&nbsp;price;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(itemsBought&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;items&nbsp;bought.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Money&nbsp;left&nbsp;over:&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;funds&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;cents</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV><BR><STRONG><FONT color=#ff0000>Item 32: Avoid strings where other types are more appropriate</FONT></STRONG><BR><FONT color=#000000>Strings are poor substitutes for other value types. More generally, if there's an appropriate value type, whether<BR>primitive or object reference, you should use it; if there isn't, you should write one.</FONT><BR><BR>Strings are poor substitutes for aggregate types. A better approach is simply to write a class to represent the aggregate, often a private static member class<BR><BR>Strings are poor substitutes for capabilities. Occasionally, strings are used to grant access to some functionality, replace the string with an unforgeable key (sometimes called a capability).<BR><BR><STRONG><FONT color=#ff0000>Item 33: Beware the performance of string concatenation</FONT></STRONG><BR><FONT color=#000000>Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuffer's append method instead. Alternatively, use a character array, or process the strings one at a time instead of combining them.<BR><BR></FONT><FONT color=#ff0000><STRONG>Item 34: Refer to objects by their interfaces<BR></STRONG></FONT><FONT color=#000000>If appropriate interface types exist, parameters, return values, variables, and fields should all be declared using interface types.<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Good&nbsp;-&nbsp;uses&nbsp;interface&nbsp;as&nbsp;type</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">List&nbsp;subscribers&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Vector();</SPAN></DIV>rather than this:<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Bad&nbsp;-&nbsp;uses&nbsp;class&nbsp;as&nbsp;type!</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">Vector&nbsp;subscribers&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Vector();</SPAN></DIV></FONT>If you get into the habit of using interfaces as types, your program will be much more flexible. For example, the first<BR>declaration could be changed to read<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">List&nbsp;subscribers&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList();</SPAN></DIV>There is one caveat: If the original implementation offered some special functionality not required by the general contract of the interface and the code depended on that functionality, then it is critical that the new implementation provide the same functionality. For example, if the code surrounding the first declaration depended on the fact that Vector is synchronized,<BR>then it would be incorrect to substitute ArrayList for Vector in the declaration.<BR><BR>It is entirely appropriate to refer to an object by a class rather than an interface if no appropriate interface exists.<BR>For example, it is perfectly appropriate to use a value class as a parameter, variable, field, or return type.<BR><BR>If an object belongs to such a <EM>class-based framework</EM>, it is preferable to refer to it by the relevant <EM>base class</EM>, which is<BR>typically abstract, rather than by its implementation class.<BR><BR>A final case in which there is no appropriate interface type is that of classes that implement an interface but provide extra methods not found in the interface—for example, LinkedList. Such a class should be used only to refer to its instances if the program relies on the extra methods: it should never be used as a parameter type (Item 25).<BR><BR><FONT color=#ff0000><STRONG>Item 35: Prefer interfaces to reflection<BR></STRONG></FONT><FONT color=#000000>Reflection comes at a price:<BR>&nbsp;&nbsp;&nbsp;You lose all the benefits of compile-time type checking.<BR>&nbsp;&nbsp;&nbsp;The code required to perform reflective access is clumsy and verbose.<BR>&nbsp;&nbsp;&nbsp;Performance suffers.<BR><BR>As a rule, objects should not be accessed reflectively in normal applications at run time.<BR><BR>You can obtain many of the benefits of reflection while incurring few of its costs by using it only in a very limited form. For many programs that must use a class unavailable at compile time, there exists at compile time an appropriate interface or superclass by which to refer to the class (Item 34). If this is the case, you can create instances reflectively and access them normally via their interface or superclass. If the appropriate constructor has no parameters, as is usually the case, then you don't even need to use the java.lang.reflect package; the Class.newInstance method provides the required functionality.<BR>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Reflective&nbsp;instantiation&nbsp;with&nbsp;interface&nbsp;access</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_89_656_Open_Image onclick="this.style.display='none'; Codehighlighter1_89_656_Open_Text.style.display='none'; Codehighlighter1_89_656_Closed_Image.style.display='inline'; Codehighlighter1_89_656_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_89_656_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_89_656_Closed_Text.style.display='none'; Codehighlighter1_89_656_Open_Image.style.display='inline'; Codehighlighter1_89_656_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</SPAN><SPAN id=Codehighlighter1_89_656_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_89_656_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Translate&nbsp;the&nbsp;class&nbsp;name&nbsp;into&nbsp;a&nbsp;class&nbsp;object</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">Class&nbsp;cl&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_160_191_Open_Image onclick="this.style.display='none'; Codehighlighter1_160_191_Open_Text.style.display='none'; Codehighlighter1_160_191_Closed_Image.style.display='inline'; Codehighlighter1_160_191_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_160_191_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_160_191_Closed_Text.style.display='none'; Codehighlighter1_160_191_Open_Image.style.display='inline'; Codehighlighter1_160_191_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_160_191_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_160_191_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>cl&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;Class.forName(args[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">]);<BR><IMG id=Codehighlighter1_225_283_Open_Image onclick="this.style.display='none'; Codehighlighter1_225_283_Open_Text.style.display='none'; Codehighlighter1_225_283_Closed_Image.style.display='inline'; Codehighlighter1_225_283_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_225_283_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_225_283_Closed_Text.style.display='none'; Codehighlighter1_225_283_Open_Image.style.display='inline'; Codehighlighter1_225_283_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(ClassNotFoundException&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_225_283_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_225_283_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.err.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Class&nbsp;not&nbsp;found.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.exit(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Instantiate&nbsp;the&nbsp;class</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">Set&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_328_358_Open_Image onclick="this.style.display='none'; Codehighlighter1_328_358_Open_Text.style.display='none'; Codehighlighter1_328_358_Closed_Image.style.display='inline'; Codehighlighter1_328_358_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_328_358_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_328_358_Closed_Text.style.display='none'; Codehighlighter1_328_358_Open_Image.style.display='inline'; Codehighlighter1_328_358_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_328_358_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_328_358_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(Set)&nbsp;cl.newInstance();<BR><IMG id=Codehighlighter1_392_455_Open_Image onclick="this.style.display='none'; Codehighlighter1_392_455_Open_Text.style.display='none'; Codehighlighter1_392_455_Closed_Image.style.display='inline'; Codehighlighter1_392_455_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_392_455_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_392_455_Closed_Text.style.display='none'; Codehighlighter1_392_455_Open_Image.style.display='inline'; Codehighlighter1_392_455_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(IllegalAccessException&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_392_455_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_392_455_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.err.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Class&nbsp;not&nbsp;accessible.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.exit(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_489_554_Open_Image onclick="this.style.display='none'; Codehighlighter1_489_554_Open_Text.style.display='none'; Codehighlighter1_489_554_Closed_Image.style.display='inline'; Codehighlighter1_489_554_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_489_554_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_489_554_Closed_Text.style.display='none'; Codehighlighter1_489_554_Open_Image.style.display='inline'; Codehighlighter1_489_554_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(InstantiationException&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_489_554_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_489_554_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.err.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Class&nbsp;not&nbsp;instantiable.</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.exit(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Exercise&nbsp;the&nbsp;set</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">s.addAll(Arrays.asList(args).subList(</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,&nbsp;args.length</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>System.out.println(s);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV></FONT><BR><FONT color=#ff0000><STRONG>Item 36: Use native methods judiciously<BR></STRONG></FONT><FONT color=#000000>Think twice before using native methods. Rarely, if ever, use them for improved performance. If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly. A single bug in the native code can corrupt your entire application.<BR><BR></FONT><FONT color=#ff0000><STRONG>Item 37: Optimize judiciously<BR></STRONG></FONT><FONT color=#000000>Strive to write good programs rather than fast ones.<BR>Strive to avoid design decisions that limit performance.<BR>Consider the performance consequences of your API design decisions.<BR>It is a very bad idea to warp an API to achieve good performance.<BR>Measure performance before and after each attempted optimization.<BR><BR></FONT><FONT color=#ff0000><STRONG>Item 38: Adhere to generally accepted naming conventions<BR></STRONG></FONT><FONT color=#000000>Internalize the standard naming conventions and learn to use them as second nature. The typographical conventions are straightforward and largely unambiguous; the grammatical conventions are more complex and looser. To quote from <A href="http://java.sun.com/docs/books/jls/">The Java Language Specification</A>, "These conventions should not be followed slavishly if long-held conventional usage dictates otherwise." Use common sense.</FONT><img src ="http://www.blogjava.net/cisco/aggbug/24448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-18 06:26 <a href="http://www.blogjava.net/cisco/articles/24448.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Effective Java》Chapter 6</title><link>http://www.blogjava.net/cisco/articles/24447.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 17 Dec 2005 22:25:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/24447.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24447.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/24447.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24447.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24447.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 6. MethodsItem 23: Check parameters for validityEach time you write a method or constructor, you should think about what restrictions exist on its parameters. You should document these restric...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/24447.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/24447.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-18 06:25 <a href="http://www.blogjava.net/cisco/articles/24447.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Effective Java》Chapter 4</title><link>http://www.blogjava.net/cisco/articles/24446.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 17 Dec 2005 22:24:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/24446.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24446.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/24446.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24446.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24446.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 4: Classes and InterfacesItem 12: Minimize the accessibility of classes and membersThe rule of thumb is that you should make each class or member as inaccessible as possible.For members (field...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/24446.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/24446.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-18 06:24 <a href="http://www.blogjava.net/cisco/articles/24446.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Effective Java》Chapter 3</title><link>http://www.blogjava.net/cisco/articles/24445.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 17 Dec 2005 22:23:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/24445.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24445.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/24445.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24445.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24445.html</trackback:ping><description><![CDATA[<FONT color=#800080><STRONG>Chapter 3. Methods Common to All Objects<BR><BR></STRONG></FONT><STRONG><FONT color=#ff0000>Item 7: Obey the general contract when overriding</STRONG> equals<BR></FONT>This is the right thing to do if any of the following conditions apply:<BR>&nbsp;&nbsp;&nbsp;a) Each instance of the class is inherently unique.<BR>&nbsp;&nbsp;&nbsp;b) You don't care whether the class provides a "logical equality" test.<BR>&nbsp;&nbsp;&nbsp;c) A superclass has already overridden equals, and the behaviour inherited from the superclass is appropriate for this class.<BR>&nbsp;&nbsp;&nbsp;d) The class is private or package-private, and you are certain that its equals method will never be invoked.<BR><BR>Here's a recipe for a high-quality equals method:<BR>&nbsp;&nbsp;&nbsp;1) Use the == operator to check if the argument is a reference to this object.<BR>&nbsp;&nbsp;&nbsp;2) Use the instanceof operator to check if the argument is of the correct type.<BR>&nbsp;&nbsp;&nbsp;3) Cast the argument to the correct type.<BR>&nbsp;&nbsp;&nbsp;4) For each "significant" field in the class, check to see if that field of the argument matches the corresponding field of this object.<BR>&nbsp;&nbsp;&nbsp;5) When you are finished writing your equals method, ask yourself three questions: Is it symmetric, is it transitive, and is it consistent?<BR><BR>Here are a few final caveats:<BR>&nbsp;&nbsp;&nbsp;a) Always override hashCode when you override equals.<BR>&nbsp;&nbsp;&nbsp;b) Don't try to be too clever.<BR>&nbsp;&nbsp;&nbsp;c) Don't write an equals method that relies on unreliable resources.<BR>&nbsp;&nbsp;&nbsp;d) Don't substitute another type for Object in the equals declaration.<BR><BR><FONT color=#ff0000><STRONG>Item 8: Always override</STRONG> hashCode <STRONG>when you override</STRONG> equals</FONT><BR>You must override hashCode in every class that overrides equals.<BR>The key provision that is violated when you fail to override hashCode is the second one:<BR>Equal objects must have equal hash codes.<BR><BR>Here is a simple recipe:<BR><STRONG>1.</STRONG> Store some constant nonzero value, say 17, in an int variable called result.<BR><STRONG>2.</STRONG> For each significant field f in your object (each field taken into account by the equals method, that is), do the following:<BR>&nbsp;&nbsp;&nbsp;<STRONG>a.</STRONG> Compute an int hash code c for the field:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>i.</STRONG> If the field is a boolean, compute (f ? 0 : 1).<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>ii.</STRONG> If the field is a byte, char, short, or int, compute (int)f.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>iii.</STRONG> If the field is a long, compute (int)(f ^ (f &gt;&gt;&gt; 32)).<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>iv.</STRONG> If the field is a float compute Float.floatToIntBits(f).<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>v.</STRONG> If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step 2.a.iii.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>vi.</STRONG> If the field is an object reference and this class's equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a "canonical representation" for this field and invoke hashCode on the canonical representation. If the value of the field is null, return 0 (or some other constant, but 0 is traditional).<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG>vii.</STRONG> If the field is an array, treat it as if each element were a separate field. That is, compute a hash code for each significant element by applying these rules recursively, and combine these values as described in step 2.b.<BR>&nbsp;&nbsp;&nbsp;<STRONG>b.</STRONG> Combine the hash code c computed in step a into result as follows: result = 37*result + c;<BR><STRONG>3.</STRONG> Return result.<BR><STRONG>4.</STRONG> When you are done writing the hashCode method, ask yourself whether equal<BR>instances have equal hash codes. If not, figure out why and fix the problem<BR><BR>Do not be tempted to exclude significant parts of an object from the hash code computation to improve performance.<BR><BR><FONT color=#ff0000><STRONG>Item 9: Always override</STRONG> toString</FONT><BR>Providing a good toString implementation makes your class much more pleasant to use.<BR>When practical, the toString method should return all of the interesting information contained in the object.<BR>Whether or not you decide to specify the format, you should clearly document your intentions.<BR>It is always a good idea to provide programmatic access to all of the information contained in the value returned by toString.<BR><BR><FONT color=#ff0000><STRONG>Item 10: Override</STRONG> clone <STRONG>judiciously</STRONG></FONT><BR>If you override the clone method in a nonfinal class, you should return an object obtained by invoking super.clone.<BR>In practice, a class the implements Cloneable is expected to provide a properly functioning public clone method. 
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_22_155_Open_Image onclick="this.style.display='none'; Codehighlighter1_22_155_Open_Text.style.display='none'; Codehighlighter1_22_155_Closed_Image.style.display='inline'; Codehighlighter1_22_155_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_22_155_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_22_155_Closed_Text.style.display='none'; Codehighlighter1_22_155_Open_Image.style.display='inline'; Codehighlighter1_22_155_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;Object&nbsp;clone()&nbsp;</SPAN><SPAN id=Codehighlighter1_22_155_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_22_155_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_29_56_Open_Image onclick="this.style.display='none'; Codehighlighter1_29_56_Open_Text.style.display='none'; Codehighlighter1_29_56_Closed_Image.style.display='inline'; Codehighlighter1_29_56_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_29_56_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_29_56_Closed_Text.style.display='none'; Codehighlighter1_29_56_Open_Image.style.display='inline'; Codehighlighter1_29_56_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_29_56_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_29_56_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">super</SPAN><SPAN style="COLOR: #000000">.clone();<BR><IMG id=Codehighlighter1_94_153_Open_Image onclick="this.style.display='none'; Codehighlighter1_94_153_Open_Text.style.display='none'; Codehighlighter1_94_153_Closed_Image.style.display='inline'; Codehighlighter1_94_153_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_94_153_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_94_153_Closed_Text.style.display='none'; Codehighlighter1_94_153_Open_Image.style.display='inline'; Codehighlighter1_94_153_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(CloneNotSupportedException&nbsp;e)&nbsp;</SPAN><SPAN id=Codehighlighter1_94_153_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_94_153_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;Error(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Assertion&nbsp;failure</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Can't&nbsp;happen</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>
<P>If, however, your object contains fields that refer to mutable objects, using this clone<BR>implementation can be disastrous. <FONT color=#008000>Neglect the rest, refer to the book for more details...</FONT></P>
<P><FONT color=#ff0000><STRONG>Item 11: Consider implementing</STRONG> Comparable<BR></FONT><FONT color=#000000>The compareTo method is not declared in Object. Rather, it is the sole method in the java.lang.Comparable interface.<BR><FONT color=#ff1493><FONT color=#008000>Array element comparisons of Chapter 11: Collections of Objects in &lt;&lt;Thinking in Java&gt;&gt; 3rd ed. Revision 4.0,&nbsp;has a more practical way to explain this.</FONT>&nbsp;</FONT></FONT></P><img src ="http://www.blogjava.net/cisco/aggbug/24445.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-18 06:23 <a href="http://www.blogjava.net/cisco/articles/24445.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Effective Java》Chapter 2</title><link>http://www.blogjava.net/cisco/articles/24444.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 17 Dec 2005 22:22:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/24444.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/24444.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/24444.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/24444.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/24444.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 2. Creating and Destroying ObjectsItem 1: Consider providing static factory methods instread of constructseg. A static factory method from the class Boolean, which translates a boolean primiti...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/24444.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/24444.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cisco/" target="_blank">Scott@JAVA</a> 2005-12-18 06:22 <a href="http://www.blogjava.net/cisco/articles/24444.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>