﻿<?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-文章分类-Java 5.0 Tiger</title><link>http://www.blogjava.net/cisco/category/6318.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:41 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 13:00:41 GMT</pubDate><ttl>60</ttl><item><title>《Java 5.0 Tiger》Chapter 4</title><link>http://www.blogjava.net/cisco/articles/26211.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sat, 31 Dec 2005 15:17:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/26211.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/26211.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/26211.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/26211.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/26211.html</trackback:ping><description><![CDATA[<FONT color=#800080>Chapter 4. Autoboxing and Unboxing<BR></FONT><BR><FONT color=#008000>4.1 Converting Primitives to Wrapper Types<BR></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: #000000">Integer&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">;</SPAN></DIV><BR>
<P class=docText>Behind the scenes, primitive values are <SPAN class=docEmphasis>boxed</SPAN>. Boxing refers to the conversion from a primitive to its corresponding wrapper type: <TT>Boolean</TT>, <TT>Byte, Short, Character, Integer, Long, Float</TT>, or <TT>Double</TT>. Because this happens automatically, it's <A name=javaadn-CHP-4-ITERM-011></A>generally referred to as <SPAN class=docEmphasis>autoboxing</SPAN>.<BR><BR>It's also common for Java to perform a widening conversion in addition to boxing a value:</P>
<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">Number&nbsp;n&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0.0f</SPAN><SPAN style="COLOR: #000000">;</SPAN></DIV><BR>Here, the literal is boxed into a <TT>Float</TT>, and then widened into a <TT>Number</TT>.<BR><BR>
<P class=docText>Additionally, the Java specification indicates that certain primitives are always to be boxed into the same <FONT color=#ff0000>immutable wrapper objects</FONT>. These objects are then cached and reused, with the expectation that these are commonly used objects. These special values are the <TT><FONT color=#ff0000>boolean</FONT></TT> values <TT>true</TT> and <TT>false</TT>, <FONT color=#ff0000>all <TT>byte</TT></FONT> values, <FONT color=#ff0000>short</FONT> and <TT><FONT color=#ff0000>int</FONT></TT> values <FONT color=#ff0000>between -128 and 127</FONT>, and any <TT><FONT color=#ff0000>char</FONT></TT> in the range <FONT color=#ff0000><TT>\u0000</TT> to <TT>\u007F</TT></FONT>.<BR><BR><FONT color=#008000>4.2 Coverting Wrapper Types to Primitives</FONT></P>
<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;Boxing</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;foo&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/None.gif" align=top>Integer&nbsp;integer&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;foo;<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;Simple&nbsp;Unboxing</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;bar&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;integer;<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>Integer&nbsp;counter&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;boxing</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;counter2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;counter;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;unboxing</SPAN></DIV><BR>...<TT>null</TT> value assignment? Since <TT>null</TT> is a legal value for an object, and therefore any wrapper type, the following code is legal:<BR><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">Integer&nbsp;i&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 src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;j&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;i;&nbsp;</SPAN></DIV><BR>
<P class=docText><TT>i</TT> is assigned <TT>null</TT> (which is legal), and then <TT>i</TT> is unboxed into <TT>j</TT>. However, <TT>null</TT> isn't a legal value for a primitive, so this code throws a <TT>NullPointerException</TT>.<BR><BR><FONT color=#008000>4.3 Incrementing and Decrementing Wrapper Types</FONT><BR><BR>Suddenly, every operation available to a primitive should be available to its wrapper-type counterpart, and vice versa.</P>
<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">Integer&nbsp;counter&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">;<BR><IMG id=Codehighlighter1_36_125_Open_Image onclick="this.style.display='none'; Codehighlighter1_36_125_Open_Text.style.display='none'; Codehighlighter1_36_125_Closed_Image.style.display='inline'; Codehighlighter1_36_125_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_36_125_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_36_125_Closed_Text.style.display='none'; Codehighlighter1_36_125_Open_Image.style.display='inline'; Codehighlighter1_36_125_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">)&nbsp;</SPAN><SPAN id=Codehighlighter1_36_125_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_36_125_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;&nbsp;&nbsp;&nbsp;&nbsp;System.out.printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Iteration&nbsp;%d%n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;counter</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;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(counter&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1000</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">break</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN></DIV><BR>Remember that <TT>counter</TT> is an <TT>Integer</TT>. So the value in <TT>counter</TT> was first auto-unboxed into an <TT>int</TT>, as that's the type required for the <TT>++</TT> operator. Once the value is unboxed, it is incremented. Then, the new value has to be stored back in <TT>counter</TT>, which requires a boxing operation. All this in a fraction of a second!<BR><BR><FONT color=#008000>4.4 Boolean Versus boolean<BR></FONT><BR>Any time you have an expression that uses <TT>!</TT>, <TT>||</TT>, or <TT>&amp;&amp;</TT>, any <TT>Boolean</TT> values are unboxed to <TT>boolean</TT> primitive values, and evaluated accordingly:<BR><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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boolean&nbsp;case1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boolean&nbsp;case2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">&nbsp;case3&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boolean&nbsp;result&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;(case1&nbsp;</SPAN><SPAN style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000">&nbsp;case2)&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000">&nbsp;case3;</SPAN></DIV><BR>
<P class=docText>For inquiring minds, primitives are boxed up to wrapper types in equality comparisons. For operators such as &lt;, &gt;=, and so forth, the wrapper types are unboxed to primitive types.<BR><BR><FONT color=#ff0000>Atten!</FONT> ...direct object comparison? <BR><BR>You can't depend on&nbsp;the result; it's merely used as an illustration. Some JVMs may choose to try and optimize this code, and create one instance for both <TT>Integer</TT> objects, and in that case, the <TT>==</TT> operator would return a true result.</P>
<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"><FONT color=#800080><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></FONT><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;i1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;i2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">256</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;i3&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 src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;i4&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 src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i1&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;i2);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;false</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i3&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;i4);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;true</SPAN></DIV><BR>If you wonder the result above, check the last paragraph in <FONT color=#008000>4.1 Converting Primitives to Wrapper Types</FONT>.<BR><BR><FONT color=#008000>4.5 Conditionals and Unboxing<BR><BR></FONT><FONT color=#000000>Ternary operator --- [conditional expression] ? [expression1] : [expression2]<BR><BR>[expression1] and [expression2] had to either be of the same type, or one had to be assignable to the other.</FONT><SPAN class=docEmphasis><FONT face="Courier New"><SPAN class=docEmphasis><FONT face="Times New Roman"><BR><BR>Another addition to Tiger is automatic casting of reference to their intersection type. That's a mouthful, so here's an example:<BR><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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;s&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hello</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer&nbsp;sb&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;StringBuffer(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">world</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">boolean</SPAN><SPAN style="COLOR: #000000">&nbsp;mutable&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CharSequence&nbsp;cs&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;mutable&nbsp;</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000">&nbsp;sb&nbsp;:&nbsp;s;</SPAN></DIV></FONT></SPAN></FONT></SPAN><BR>In pre-Tiger environments, this would generate an error, as <TT>sb</TT> (a <TT>StringBuffer</TT>) and <TT>s</TT> (a <TT>String</TT>) cannot be assigned to each other, unless you explicitly cast both to CharSequence.<BR><BR>
<P class=docText>As a side effect of this, note that two reference types (objects) <SPAN class=docEmphasis>always</SPAN> share <TT>java.lang.Object</TT> as a common ancestor, so any result of a ternary operation involving non-primitive operands can be assigned to <TT>java.lang.Object</TT>.<BR><BR><FONT color=#008000>4.6 Control Statements and Unboxing</FONT><BR><BR>if/else, while, and do all are affected by Tiger's ability to unbox Boolean values to boolean values.<BR><BR>Another statement that benefits from unboxing is <TT>switch</TT>. In pre-Tiger JVMs, the <TT>switch</TT> statement accepts <TT>int, short, char</TT>, or <TT>byte</TT> values. With unboxing in play, you can now supply it with <TT>Integer, Short, Char</TT>, and <TT>Byte</TT> values as well, in addition to the introduction of <TT>enums</TT>.<BR><BR><FONT color=#008000>4.7 Method Overload Resolution<BR></FONT><BR></P>
<P class=docText>In Tiger, method resolution is a three-pass process:</P><SPAN style="FONT-WEIGHT: bold">
<OL class=docList type=1>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>The compiler attempts to locate the correct method without any boxing, unboxing, or vararg invocations. This will find any method that would have been invoked under Java 1.4 rules.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>If the first pass fails, the compiler tries method resolution again, this time allowing boxing and unboxing conversions. Methods with varargs are <SPAN class=docEmphasis>not</SPAN> considered in this pass.</P></SPAN>
<LI><SPAN style="FONT-WEIGHT: normal">
<P class=docList>If the second pass fails, the compiler tries method resolution one last time, allowing boxing and unboxing, and also considers vararg methods.</P></SPAN></LI></OL></SPAN>
<P class=docText>These rules ensure that consistency with pre-Tiger environments is maintained.</P><img src ="http://www.blogjava.net/cisco/aggbug/26211.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-31 23:17 <a href="http://www.blogjava.net/cisco/articles/26211.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Java 5.0 Tiger》Chapter 3</title><link>http://www.blogjava.net/cisco/articles/25975.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Thu, 29 Dec 2005 21:28:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/25975.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/25975.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/25975.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/25975.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/25975.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 3. Enumerated Types3.1 Creating an EnumMore often than not, you'll only need the basic enum functionality:public&nbsp;enum&nbsp;Grade&nbsp;{&nbsp;A,&nbsp;B,&nbsp;C,&nbsp;D,&nbsp;F,&nbsp;IN...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/25975.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/25975.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-30 05:28 <a href="http://www.blogjava.net/cisco/articles/25975.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Java 5.0 Tiger》Chapter 2</title><link>http://www.blogjava.net/cisco/articles/25522.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Mon, 26 Dec 2005 19:30:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/25522.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/25522.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/25522.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/25522.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/25522.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 2. Generics 2.1 Using Type-Safe Lists In pre-Tiger versions of Java, the method signature for add() in List looked like this:public&nbsp;boolean&nbsp;add(Object&nbsp;obj);In Tiger, thoug...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/25522.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/25522.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-27 03:30 <a href="http://www.blogjava.net/cisco/articles/25522.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《Java 5.0 Tiger》Chapter 1</title><link>http://www.blogjava.net/cisco/articles/25387.html</link><dc:creator>Scott@JAVA</dc:creator><author>Scott@JAVA</author><pubDate>Sun, 25 Dec 2005 14:24:00 GMT</pubDate><guid>http://www.blogjava.net/cisco/articles/25387.html</guid><wfw:comment>http://www.blogjava.net/cisco/comments/25387.html</wfw:comment><comments>http://www.blogjava.net/cisco/articles/25387.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cisco/comments/commentRss/25387.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cisco/services/trackbacks/25387.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Chapter 1. What's New?1.1 Working with ArraysString[]&nbsp;s1&nbsp;=&nbsp;{&nbsp;"1",&nbsp;"2",&nbsp;"3"&nbsp;};System.out.println(Arrays.toString(s1));String[][]&nbsp;s2&nbsp;=&nbsp;{&nbsp;{&nbsp;"...&nbsp;&nbsp;<a href='http://www.blogjava.net/cisco/articles/25387.html'>阅读全文</a><img src ="http://www.blogjava.net/cisco/aggbug/25387.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-25 22:24 <a href="http://www.blogjava.net/cisco/articles/25387.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>