﻿<?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-今天是昨天的明天</title><link>http://www.blogjava.net/cjj3930337/</link><description>天行建，君子以自强不息!</description><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 02:03:02 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 02:03:02 GMT</pubDate><ttl>60</ttl><item><title>jdk1.5的新特性</title><link>http://www.blogjava.net/cjj3930337/archive/2011/08/24/357195.html</link><dc:creator>じ蓝雨☆新</dc:creator><author>じ蓝雨☆新</author><pubDate>Wed, 24 Aug 2011 07:53:00 GMT</pubDate><guid>http://www.blogjava.net/cjj3930337/archive/2011/08/24/357195.html</guid><wfw:comment>http://www.blogjava.net/cjj3930337/comments/357195.html</wfw:comment><comments>http://www.blogjava.net/cjj3930337/archive/2011/08/24/357195.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/cjj3930337/comments/commentRss/357195.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/cjj3930337/services/trackbacks/357195.html</trackback:ping><description><![CDATA[<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">1. </span></strong><strong><span style="
line-height:150%;font-family:宋体;">泛型 </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%"><strong><span style="line-height:150%;font-family:宋体;">2 </span></strong><strong><span style="line-height:150%;font-family:宋体;">自动装箱/拆箱 </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%"><strong><span style="line-height:150%;font-family:宋体;">3 for-each </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%"><strong><span style="line-height:150%;font-family:宋体;">4 static import </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%"><strong><span style="line-height:150%;font-family:宋体;">5 </span></strong><strong><span style="line-height:150%;font-family:宋体;">变长参数 </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">1. </span></strong><strong><span style="
line-height:150%;font-family:宋体;">泛型
（避免类型强制转换可能引起的运行错误） </span></strong></p>
<p style="margin-top:6.0pt;line-height:
150%"><span style="line-height:150%;font-family:宋体;">例如: </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">ArrayList list=new ArrayList(); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">list.add(new Integer(3)); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">list.add(new Integer(4)); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">int
i=((Integer)(list.get(0))).parseInt(); </span></p>
<p style="text-indent:5.25pt;
line-height:150%"><span style="line-height:150%;
font-family:宋体;">很麻烦 </span></p>
<p style="text-indent:21.0pt;
line-height:150%"><span style="line-height:
150%;font-family:宋体;">ArrayList&lt;Integer&gt;
list=new ArrayList&lt;Integer&gt;(); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">list.add(new Integer(3)); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">list.add(new Integer(4)); </span></p>
<p style="text-indent:21.0pt;line-height:150%"><span style="line-height:150%;font-family:宋体;">int i=list.get(0).parseInt(); </span></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">2. </span></strong><strong><span style="
line-height:150%;font-family:宋体;">自动装箱/拆箱 </span></strong></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">上面例子的最后一句可改为： </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="
line-height:150%;font-family:宋体;">int
i=list.get(0); </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">因为原始类型与对应的包装类不用显式转换
</span></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">3. for-each </span></strong></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">循环的增强 </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">int a[]={........};//</span><span style="font-family:宋体;">初始化 </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">for(int i:a) </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">{ </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">...... </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">} </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">不用以前的i=0;i&lt;a.length;i++ </span></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">4. static import </span></strong></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">以前调Java.math </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="
line-height:150%;font-family:宋体;">Math.sqrt();
</span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">现在 static import java.lang.Math.sqrt; </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">再 sqrt(); </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">相当于你自己类里有这个方法
</span></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong><span style="line-height:150%;font-family:宋体;">5. </span></strong><strong><span style="
line-height:150%;font-family:宋体;">变长参数 </span></strong></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">int sum(int ...intlist) </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">{ </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">int sum; </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">sum=0; </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">for(int i=0;i&lt;intlist.length;i++) </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">{ </span></p>
<p style="margin-left:42.0pt;text-indent:21.0pt"><span style="font-family:宋体;">sum+=intlist[i]; </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">} </span></p>
<p style="margin-left:21.0pt;text-indent:21.0pt"><span style="font-family:宋体;">return sum; </span></p>
<p style="text-indent:21.0pt"><span style="font-family:宋体;">} </span></p>
<p style="margin-top:6.0pt;text-indent:
21.0pt;line-height:150%"><span style="line-height:
150%;font-family:宋体;">有任意个参数,把他看作数组 </span></p>
<p style="margin-top:6.0pt;line-height:
150%;"><strong style><span style='line-height: 150%; font-family: "宋体"'><br />
</span></strong></p>
<img src ="http://www.blogjava.net/cjj3930337/aggbug/357195.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/cjj3930337/" target="_blank">じ蓝雨☆新</a> 2011-08-24 15:53 <a href="http://www.blogjava.net/cjj3930337/archive/2011/08/24/357195.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>