﻿<?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-JafeLee-随笔分类-Book Notes-- Java Puzzlers</title><link>http://www.blogjava.net/JafeLee/category/24815.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 28 Aug 2007 01:26:20 GMT</lastBuildDate><pubDate>Tue, 28 Aug 2007 01:26:20 GMT</pubDate><ttl>60</ttl><item><title>Puzzlers with Character (1)</title><link>http://www.blogjava.net/JafeLee/archive/2007/08/26/139649.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sun, 26 Aug 2007 14:53:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/08/26/139649.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/139649.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/08/26/139649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/139649.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/139649.html</trackback:ping><description><![CDATA[
		<div align="center">Puzzle 11:<br /><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_224823" onclick="this.style.display='none'; Code_Closed_Text_224823.style.display='none'; Code_Open_Image_224823.style.display='inline'; Code_Open_Text_224823.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_224823" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_224823.style.display='none'; Code_Closed_Image_224823.style.display='inline'; Code_Closed_Text_224823.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_224823" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">LastLaugh.java</span><span id="Code_Open_Text_224823" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> LastLaugh<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String args[])<br />    {<br />        System.out.print(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">H</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Ha</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">H</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">169</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div>但 + 和char型同时出现的时候，分清运算是字符串连接运算还是简单的算术运算。<br />JSL 5.1.2、5.6.2<br /><div align="center">Puzzle 12:<br /><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_092315" onclick="this.style.display='none'; Code_Closed_Text_092315.style.display='none'; Code_Open_Image_092315.style.display='inline'; Code_Open_Text_092315.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_092315" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_092315.style.display='none'; Code_Closed_Image_092315.style.display='inline'; Code_Closed_Text_092315.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_092315" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Abc.java</span><span id="Code_Open_Text_092315" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Abc<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        String letters </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ABC</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">[] numbers </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">};<br />        </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> [] chars </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">b</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">};<br />        System.out.println(letters </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">easy as </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> numbers); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> ABCeasy as [C@de6ced</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(letters </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> easy as </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> chars); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> ABC easy as [C@c17164</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(numbers); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 123</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(chars); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> abc</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div>关于数组的，参见JSL 10<br /></div></div></div></div>
<img src ="http://www.blogjava.net/JafeLee/aggbug/139649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-08-26 22:53 <a href="http://www.blogjava.net/JafeLee/archive/2007/08/26/139649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Expressive Puzzlers (2)</title><link>http://www.blogjava.net/JafeLee/archive/2007/08/23/138900.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Thu, 23 Aug 2007 09:56:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/08/23/138900.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/138900.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/08/23/138900.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/138900.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/138900.html</trackback:ping><description><![CDATA[
		<div align="center">Puzzle 8:<br /><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_183153" onclick="this.style.display='none'; Code_Closed_Text_183153.style.display='none'; Code_Open_Image_183153.style.display='inline'; Code_Open_Text_183153.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_183153" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_183153.style.display='none'; Code_Closed_Image_183153.style.display='inline'; Code_Closed_Text_183153.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_183153" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">DosEquiz.java</span><span id="Code_Open_Text_183153" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> DosEquis<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">X</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">89</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> j </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">89</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> k </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">6666666</span><span style="color: rgb(0, 0, 0);">;<br />        System.out.println(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> x : </span><span style="color: rgb(0, 0, 0);">88</span><span style="color: rgb(0, 0, 0);">);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> X</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> i : x); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 88</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        <br />        System.out.println(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> j : x); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Y</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> j : x); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> X</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> k : x);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 6666666</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> k : x);  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 88<br />        <br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">System.out.println((true ? true : new Boolean(true)).booleanValue()); \\ a compile-time error <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">System.out.println((true ? false : new Boolean(true)).booleanValue()); \\ a compile-time error </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Boolean((</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">?</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);"> : </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Boolean(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">))).booleanValue()); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">ok<br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Double aInt = true ? new Integer(1) : null; -- a compile-time error <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Double aInt = false ? new Integer(1) : null; -- a compile-time error </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        <br />        <br />    }<br />}<br /></span></span></div><br />这个谜题是关于 <font color="#006400"><i>Conditional Operator </i><tt><i>? :</i></tt></font>[JLS 15.25]。<br />1、Conditional Operator 有三个运算符表达式，第一个必须是 boolean或 Boolean型的，否则引发一个compile-time error 。<br />2、一个 Conditional Expression的类型T是由第二个表达式 T2和第三个表达式T3的类型来决定的。具体规则如下：<br /><ul><li>rule 1:  如果T2 == T3（包括T2 == T3 == null的情况），那么 T = T2（T3）。</li><li>rule 2:  如果 T2与T3这两个之中有一个是boolean型，另外一个是Boolean型，那么 T = boolean。</li><li>rule 3:  如果T2与T3之中有一个是null类型，而另外一个是一个引用类型(reference type) RT，那么T = RT</li><li>如果T2与T3是可以转化成数值类型的，那么结果有以下几种情况：</li><ol><li>rule 4:  如果T2与T3有一个是byte或者Byte，另一个是short或者Short，那么T = short</li><li>rule 5:  如果T2与T3中有一个是 GT 型，这里 GT 泛指byte, short, char中的任何一个, 而且另一个是一个int型的常量表达式(注意，必须是一个 int型的constant expression)，而且 该常量表达式可以用GT表示（即不会产生精度损失的情况，个人的理解^_^），则T=GT。</li><li>rule 6： 如果T2和T3有一个是Byte型，另一个是可以用byte型表示的int型常量表达式，那么T=byte</li><li>rule 7： 如果T2和T3有一个是Short型，另一个是可以用byte型表示的int型常量表达式，那么T=short</li><li>rule 8： 如果T2和T3有一个是Character型，另一个是可以用char型表示的int型常量表达式，那么T=char</li><li>rule 9:  Otherwise, binary numeric promotion (<span class="docLink">JLS 5.6.2</span>) is applied to the operand 
types, and the type of the conditional expression is the promoted type of the 
second and third operands. Note that binary numeric promotion performs unboxing 
conversion (JLS <span class="docLink">5.1.8</span>) 
and value set conversion (JLS <span class="docLink">5.1.13</span>).(这个很多词语都不知该怎么翻译，就直接copy过了。。。）</li></ol><li><p class="docList">rule 10:  Otherwise, the second and third operands are of types <span class="docEmphasis">S1</span> and <span class="docEmphasis">S2</span> respectively. 
Let <span class="docEmphasis">T1</span> be the type that results from applying 
boxing conversion to <span class="docEmphasis">S1</span>, and let <span class="docEmphasis">T2</span> be the type that results from applying boxing 
conversion to <span class="docEmphasis">S2</span>. The type of the conditional 
expression is the result of applying capture conversion (JLS <span class="docLink">5.1.10</span>) to <span class="docEmphasis">lub(T1, T2)</span> (JLS <span class="docLink">15.12.2.7</span>).</p></li></ul><br /><div align="center">Puzzle 9:<br /><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_152238" onclick="this.style.display='none'; Code_Closed_Text_152238.style.display='none'; Code_Open_Image_152238.style.display='inline'; Code_Open_Text_152238.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_152238" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_152238.style.display='none'; Code_Closed_Image_152238.style.display='inline'; Code_Closed_Text_152238.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_152238" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Tweedledum.java</span><span id="Code_Open_Text_152238" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Tweedledum<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />        x </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> i;   </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">It is ok because it contains a hidden cast</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> i; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> a compile-error</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}</span></span></div>关于Compound Assignment Operators，参见JLS 15.26.2.<br /><div align="center">Puzzle 10:<br /><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_132910" onclick="this.style.display='none'; Code_Closed_Text_132910.style.display='none'; Code_Open_Image_132910.style.display='inline'; Code_Open_Text_132910.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_132910" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_132910.style.display='none'; Code_Closed_Image_132910.style.display='inline'; Code_Closed_Text_132910.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_132910" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Tweedledee.java</span><span id="Code_Open_Text_132910" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Tweedledee<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Put your declarations for x and i here</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />        Object x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Buy </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />        String i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Effective Java!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />        x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> i;<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Must be LEGAL</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        x </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> i;<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Must be ILLEGAL</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />    }<br />}<br /></span></span></div><br /></div></div></div></div></div></div>
<img src ="http://www.blogjava.net/JafeLee/aggbug/138900.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-08-23 17:56 <a href="http://www.blogjava.net/JafeLee/archive/2007/08/23/138900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Expressive Puzzlers (1)</title><link>http://www.blogjava.net/JafeLee/archive/2007/08/11/136015.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 11 Aug 2007 05:46:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/08/11/136015.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/136015.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/08/11/136015.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/136015.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/136015.html</trackback:ping><description><![CDATA[：		<div align="center"><font color="#ff0000">Puzzle 1:</font><br /></div><br />当求余运算(remainder operation)符 % 返回一个非零余数时，余数的符号位和左边操作数的符号位相同。例如<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">        System.out.println((</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">53</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> -8</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">53</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">)); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">8</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println((</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">53</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">)); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">-8</span></div><br /><div align="center"><font color="#ff0000">Puzzle 2:</font><br /></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_164040" onclick="this.style.display='none'; Code_Closed_Text_164040.style.display='none'; Code_Open_Image_164040.style.display='inline'; Code_Open_Text_164040.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_164040" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_164040.style.display='none'; Code_Closed_Image_164040.style.display='inline'; Code_Closed_Text_164040.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_164040" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Change.java</span><span id="Code_Open_Text_164040" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.math.BigDecimal;<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Change<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String args[])<br />    {<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">2.00</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1.10</span><span style="color: rgb(0, 0, 0);">);</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">0.8999999999999999</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> BigDecimal(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">2.00</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">).subtract(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> BigDecimal(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">1.10</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)));</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">0.90</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%.2f%n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2.00</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1.10</span><span style="color: rgb(0, 0, 0);">);</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">0.90</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div><br />关于浮点数的二进制表示～～<br />（1）二进制浮点数并不能精确表示所有的小数<br />（2）对计算精度要求比较准确（例如金融计算）时，不要使用float和double，尽量使用int, long，BigDecimal.<br />（3）推荐阅读文章：What Every Computer Scientist Should Know About Floating-Point Arithmetic<br />网上很多地方都有的。另一本牛书 Computer Systems A Programmers's Perspective上也有讲浮点数<br />（4）JLS 3.10.1由规范可知 0.1, .1, 1. 都是合法的浮点数。需要注意的是在java中，浮点数有两种原生类型float,double，当浮点数的后缀是F或者f时，该浮点数为float类型，没有后缀或者后缀是D或者d时，该浮点数是double类型的。注意下面的例子<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_185827" onclick="this.style.display='none'; Code_Closed_Text_185827.style.display='none'; Code_Open_Image_185827.style.display='inline'; Code_Open_Text_185827.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_185827" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_185827.style.display='none'; Code_Closed_Image_185827.style.display='inline'; Code_Closed_Text_185827.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_185827" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">FloatPoint.java</span><span id="Code_Open_Text_185827" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> FloatPoint<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String [] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> .</span><span style="color: rgb(0, 0, 0);">12345</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> y </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1234</span><span style="color: rgb(0, 0, 0);">.;<br />        </span><span style="color: rgb(0, 0, 255);">double</span><span style="color: rgb(0, 0, 0);"> z </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1.123432343</span><span style="color: rgb(0, 0, 0);">;<br />        <br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">float a = 0.1; -- 可能损失精度</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0.1f</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> .1234f;<br />        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">float d = .123; －－可能损失精度</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">x = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> x);<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">y = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> y);<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">z = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> z);<br />        <br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">b = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> b);<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> c);<br />    }<br />}</span></span></div><br />结果：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_185939" onclick="this.style.display='none'; Code_Closed_Text_185939.style.display='none'; Code_Open_Image_185939.style.display='inline'; Code_Open_Text_185939.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_185939" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_185939.style.display='none'; Code_Closed_Image_185939.style.display='inline'; Code_Closed_Text_185939.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_185939" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">结果</span><span id="Code_Open_Text_185939" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0.12345</span><span style="color: rgb(0, 0, 0);"><br />y </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1234.0</span><span style="color: rgb(0, 0, 0);"><br />z </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1.123432343</span><span style="color: rgb(0, 0, 0);"><br />b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0.1</span><span style="color: rgb(0, 0, 0);"><br />c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0.1234</span></span></div><br /><div align="center"><font color="#ff0000">Puzzle 3:</font><br /></div>需要注意java是如何处理整数溢出的，看下面的例子就一目了然了，别忘了long是 8 bytes，int是 4 bytes的~~<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_192116" onclick="this.style.display='none'; Code_Closed_Text_192116.style.display='none'; Code_Open_Image_192116.style.display='inline'; Code_Open_Text_192116.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_192116" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_192116.style.display='none'; Code_Closed_Image_192116.style.display='inline'; Code_Closed_Text_192116.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_192116" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">LongDividion.java</span><span id="Code_Open_Text_192116" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> LongDivision<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> MICROS_PER_DAY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000L</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> MILLIS_PER_DAY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">24</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">60</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000L</span><span style="color: rgb(0, 0, 0);">;<br /><br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> micros_per_day </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) MICROS_PER_DAY;<br />        </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> millis_per_day </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">)MILLIS_PER_DAY;<br />        <br />        System.out.println(Long.toHexString(MICROS_PER_DAY));  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">      141dd76000</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(Integer.toHexString(micros_per_day));  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">     1dd76000</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">         System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">*********************************</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        System.out.println(micros_per_day); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 500654080</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(millis_per_day); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 86400000</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div><br /><div align="center"><font color="#ff0000">Puzzle 4:</font><br /></div>添加long型整数的后缀时要使用L避免用l，同样不要单独使用小写字母l作为变量名，理由是显而易见的：l和1在大多数字体中太难区分。<br /><div align="center"><font color="#ff0000">Puzzle 5:</font><br /><div align="left">（1）和十进制数不同，当十六进制、八进制数的最高位是1时，表示它是一个负数（在十进制数中，表示一个负数要显式使用符号-)<br />（2）尽量避免混合类型运算，例如本例中的 long型和int型的加法，在java中，一个整数如果没有后缀L或l，则它是一个int型而不是long型。<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_144004" onclick="this.style.display='none'; Code_Closed_Text_144004.style.display='none'; Code_Open_Image_144004.style.display='inline'; Code_Open_Text_144004.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_144004" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_144004.style.display='none'; Code_Closed_Image_144004.style.display='inline'; Code_Closed_Text_144004.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_144004" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">JoyOfHex.java</span><span id="Code_Open_Text_144004" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> JoyOfHex<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        System.out.println(Long.toHexString(</span><span style="color: rgb(0, 0, 0);">0x100000000L</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xcafebabe</span><span style="color: rgb(0, 0, 0);">));</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">cafebabe instead of 1cafebabe</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(Long.toHexString(</span><span style="color: rgb(0, 0, 0);">0x100000000L</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0xcafebabeL</span><span style="color: rgb(0, 0, 0);">)); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 1cafebabe</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">0xffffffffL</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 4294967295</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">        System.out.println(</span><span style="color: rgb(0, 0, 0);">0xffffffff</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> -1</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div><div align="center"><font color="#ff0000">Puzzle 6:<br /></font><div align="left">The rule "<font color="#0000ff" face="Times New Roman" size="4">Sign
extension is performed if the type of the original value is signed;
zero extension if it is a char, regardless of the type to which it is
being converted</font>" describes the sign extension behavior when converting <font color="#ff1493" size="3">from narrower integral types to wider</font>.<br /><br /></div><div align="left"><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_133155" onclick="this.style.display='none'; Code_Closed_Text_133155.style.display='none'; Code_Open_Image_133155.style.display='inline'; Code_Open_Text_133155.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_133155" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_133155.style.display='none'; Code_Closed_Image_133155.style.display='inline'; Code_Closed_Text_133155.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_133155" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Multicast.java</span><span id="Code_Open_Text_133155" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Multicast<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        </span><span style="color: rgb(0, 128, 0);">/*</span><span style="color: rgb(0, 128, 0);">  int -&gt; byte : 0xffffffff -&gt; 0xff<br />        *        byte -&gt; char: 0xff -&gt; 0xffff<br />        *        char -&gt; int : 0xffff -&gt; 0x0000ffff<br />        </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />        System.out.println((</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">0x0000ffff = 65535</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div><div align="center"><font color="#ff0000">Puzzle 6:<br /></font><div align="left">JLS 15.7 Evaluation Order</div></div><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_135811" onclick="this.style.display='none'; Code_Closed_Text_135811.style.display='none'; Code_Open_Image_135811.style.display='inline'; Code_Open_Text_135811.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" height="16" width="11" /><img id="Code_Open_Image_135811" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_135811.style.display='none'; Code_Closed_Image_135811.style.display='inline'; Code_Closed_Text_135811.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" height="16" width="11" /><span id="Code_Closed_Text_135811" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">CleverSwap.java</span><span id="Code_Open_Text_135811" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> CleverSwap<br />{<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args)<br />    {<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1984</span><span style="color: rgb(0, 0, 0);">;<br />        </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> y </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2001</span><span style="color: rgb(0, 0, 0);">;<br />        x </span><span style="color: rgb(0, 0, 0);">^=</span><span style="color: rgb(0, 0, 0);"> y </span><span style="color: rgb(0, 0, 0);">^=</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">^=</span><span style="color: rgb(0, 0, 0);"> y;<br />        System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">x = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">; y = </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> y); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> x = 0; y = 1984</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    }<br />}<br /></span></span></div><br /></div></div></div></div><img src ="http://www.blogjava.net/JafeLee/aggbug/136015.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-08-11 13:46 <a href="http://www.blogjava.net/JafeLee/archive/2007/08/11/136015.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 解惑（英文版）</title><link>http://www.blogjava.net/JafeLee/archive/2007/08/11/136005.html</link><dc:creator>Jafe</dc:creator><author>Jafe</author><pubDate>Sat, 11 Aug 2007 04:40:00 GMT</pubDate><guid>http://www.blogjava.net/JafeLee/archive/2007/08/11/136005.html</guid><wfw:comment>http://www.blogjava.net/JafeLee/comments/136005.html</wfw:comment><comments>http://www.blogjava.net/JafeLee/archive/2007/08/11/136005.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/JafeLee/comments/commentRss/136005.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/JafeLee/services/trackbacks/136005.html</trackback:ping><description><![CDATA[
		<table style="margin-top: 16px;" border="0" cellpadding="0" cellspacing="0" height="206" width="733">
				<tbody>
						<tr>
								<td>【原 书 名】 </td>
								<td>
                Java Puzzlers：Traps，Pitfalls，and Corner Cases
                
              </td>
						</tr>
						<tr>
								<td>【原出版社】 </td>
								<td>Addison-Wesley</td>
						</tr>
						<tr>
								<td>【作　　者】</td>
								<td>（美）Joshua Bloch, Neal Gafter <span></span></td>
						</tr>
						<tr>
								<td>【丛 书 名】 </td>
								<td>典藏原版书苑</td>
						</tr>
						<tr>
								<td>【出 版 社】 </td>
								<td>
										<!--<a href="/search/power_search/power_search.asp?key1=人民邮电出版社" target=_blank></a> -->
                人民邮电出版社</td>
						</tr>
						<tr>
								<td>【书      号】</td>
								<td>7115149569</td>
						</tr>
						<tr>
								<td>【开    本】</td>
								<td>16开</td>
						</tr>
						<tr>
								<td>【页    码】</td>
								<td>282</td>
						</tr>
						<tr>
								<td>【出版日期】</td>
								<td>2006年7月</td>
						</tr>
						<tr>
								<td>【版    次】</td>
								<td>1-1<br /></td>
						</tr>
						<tr>
								<td valign="top">
										<br />
								</td>
								<td valign="top">
										<br />
								</td>
						</tr>
				</tbody>
		</table>
		<br />很不错的一本书，每个问题篇幅短小，问题知识性很强，同时又不乏趣味性～～<br />网上很多地方都有电子版下载，包括www.flazx.com<br />btw，Joshua Block是 Effective Java的作者<br /><br /><img src ="http://www.blogjava.net/JafeLee/aggbug/136005.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/JafeLee/" target="_blank">Jafe</a> 2007-08-11 12:40 <a href="http://www.blogjava.net/JafeLee/archive/2007/08/11/136005.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>