﻿<?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-ol_soft-随笔分类-J2SE</title><link>http://www.blogjava.net/ol-l/category/33935.html</link><description>会笑的人总会吧笑留在最后，就像春雷总是走在闪电之后，却一鸣惊人！</description><language>zh-cn</language><lastBuildDate>Mon, 20 Apr 2009 15:57:34 GMT</lastBuildDate><pubDate>Mon, 20 Apr 2009 15:57:34 GMT</pubDate><ttl>60</ttl><item><title>[转]关于六种Java异常处理程序的陋习</title><link>http://www.blogjava.net/ol-l/archive/2009/04/20/266636.html</link><dc:creator>ol_soft</dc:creator><author>ol_soft</author><pubDate>Mon, 20 Apr 2009 15:50:00 GMT</pubDate><guid>http://www.blogjava.net/ol-l/archive/2009/04/20/266636.html</guid><wfw:comment>http://www.blogjava.net/ol-l/comments/266636.html</wfw:comment><comments>http://www.blogjava.net/ol-l/archive/2009/04/20/266636.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ol-l/comments/commentRss/266636.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ol-l/services/trackbacks/266636.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana; font-size: 12px; line-height: 18px; ">
<p class="MsoNormal" align="left" style="text-align: left; word-break: break-all; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><span style="font-family: 宋体; color: #333333; ">你觉得自己是一个</span><span lang="EN-US" style="font-family: Arial; color: #333333; ">Java</span><span style="font-family: 宋体; color: #333333; ">专家吗？是否肯定自己已经全面掌握了</span><span lang="EN-US" style="font-family: Arial; color: #333333; ">Java</span><span style="font-family: 宋体; color: #333333; ">的异常处理机制？在下面这段代码中，你能够迅速找出异常处理的六个问题吗？</span><span lang="EN-US" style="font-family: Arial; color: #333333; "><o:p></o:p></span></p>
<blockquote class="webkit-indent-blockquote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 40px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span  style="color: #81888c; font-family: Arial; ">1 OutputStreamWriter out = ...</span><br />
<span  style="color: #81888c; font-family: Arial; ">2 java.sql.Connection conn = ...</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">3 try { //&#160;</span><span style="font-family: 宋体; color: #81888c; ">⑸</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">4&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Statement stat = conn.createStatement();</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">5&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ResultSet rs = stat.executeQuery(</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">6&#160;</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">"select uid, name from user");</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">7&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">while (rs.next())</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">8&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">{</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">9&#160;</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">out.println("ID</span><span style="font-family: 宋体; color: #81888c; ">：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">" + rs.getString("uid") //&#160;</span><span style="font-family: 宋体; color: #81888c; ">⑹</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">10&#160;</span><span style="font-family: 宋体; color: #81888c; ">　　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">"</span><span style="font-family: 宋体; color: #81888c; ">，姓名：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">" + rs.getString("name"));</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">11&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">}</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">12&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">conn.close(); //&#160;</span><span style="font-family: 宋体; color: #81888c; ">⑶</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">13&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">out.close();</span><br />
<span  style="color: #81888c; font-family: Arial; ">14 }</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">15 catch(Exception ex) //&#160;</span><span style="font-family: 宋体; color: #81888c; ">⑵</span><br />
<span  style="color: #81888c; font-family: Arial; ">16 {</span><br />
<span lang="EN-US" style="font-family: Arial; color: #81888c; ">17&#160;</span><span style="font-family: 宋体; color: #81888c; ">　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ex.printStackTrace(); //</span><span style="font-family: 宋体; color: #81888c; ">⑴</span><span style="font-family: 宋体; color: #81888c; ">，</span><span style="font-family: 宋体; color: #81888c; ">⑷</span><br />
<span  style="color: #81888c; font-family: Arial; ">18 }</span><br />
</blockquote>
<p class="MsoNormal" align="left" style="text-align: left; word-break: break-all; margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
</span><span style="font-family: 宋体; color: #81888c; ">　　作为一个</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">程序员，你至少应该能够找出两个问题。但是，如果你不能找出全部六个问题，请继续阅读本文。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　本文讨论的不是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">异常处理的一般性原则，因为这些原则已经被大多数人熟知。我们要做的是分析各种可称为</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8220;</span><span style="font-family: 宋体; color: #81888c; ">反例</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8221;</span><span style="font-family: 宋体; color: #81888c; ">（</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">anti-pattern</span><span style="font-family: 宋体; color: #81888c; ">）的违背优秀编码规范的常见坏习惯，帮助读者熟悉这些典型的反面例子，从而能够在实际工作中敏锐地察觉和避免这些问题。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　反例之一：丢弃异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　代码：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">15</span><span style="font-family: 宋体; color: #81888c; ">行</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">-18</span><span style="font-family: 宋体; color: #81888c; ">行。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　这段代码捕获了异常却不作任何处理，可以算得上</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">编程中的杀手。从问题出现的频繁程度和祸害程度来看，它也许可以和</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">C/C++</span><span style="font-family: 宋体; color: #81888c; ">程序的一个恶名远播的问题相提并论</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">??</span><span style="font-family: 宋体; color: #81888c; ">不检查缓冲区是否已满。如果你看到了这种丢弃（而不是抛出）异常的情况，可以百分之九十九地肯定代码存在问题（在极少数情况下，这段代码有存在的理由，但最好加上完整的注释，以免引起别人误解）。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　这段代码的错误在于，异常（几乎）总是意味着某些事情不对劲了，或者说至少发生了某些不寻常的事情，我们不应该对程序发出的求救信号保持沉默和无动于衷。调用一下</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">算不上</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8220;</span><span style="font-family: 宋体; color: #81888c; ">处理异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8221;</span><span style="font-family: 宋体; color: #81888c; ">。不错，调用</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">对调试程序有帮助，但程序调试阶段结束之后，</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">就不应再在异常处理模块中担负主要责任了。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　丢弃异常的情形非常普遍。打开</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDK</span><span style="font-family: 宋体; color: #81888c; ">的</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">类的文档，可以看到下面这段说明：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8220;</span><span style="font-family: 宋体; color: #81888c; ">特别地，虽然出现</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">是一种</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8216;</span><span style="font-family: 宋体; color: #81888c; ">正常的情形</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8217;</span><span style="font-family: 宋体; color: #81888c; ">，但</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">类是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">而不是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">的子类，因为许多应用会捕获所有的</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">然后丢弃它不再理睬。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8221;</span><span style="font-family: 宋体; color: #81888c; ">这段话的意思是，虽然</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">代表的是一种普通的问题，但鉴于许多应用会试图捕获所有异常然后不予以适当的处理，所以</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDK</span><span style="font-family: 宋体; color: #81888c; ">把</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">ThreadDeath</span><span style="font-family: 宋体; color: #81888c; ">定义成了</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">的子类，因为</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Error</span><span style="font-family: 宋体; color: #81888c; ">类代表的是一般的应用不应该去捕获的严重问题。可见，丢弃异常这一坏习惯是如此常见，它甚至已经影响到了</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">本身的设计。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　那么，应该怎样改正呢？主要有四个选择：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">1</span><span style="font-family: 宋体; color: #81888c; ">、处理异常。针对该异常采取一些行动，例如修正问题、提醒某个人或进行其他一些处理，要根据具体的情形确定应该采取的动作。再次说明，调用</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">printStackTrace</span><span style="font-family: 宋体; color: #81888c; ">算不上已经</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8220;</span><span style="font-family: 宋体; color: #81888c; ">处理好了异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8221;</span><span style="font-family: 宋体; color: #81888c; ">。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">2</span><span style="font-family: 宋体; color: #81888c; ">、重新抛出异常。处理异常的代码在分析异常之后，认为自己不能处理它，重新抛出异常也不失为一种选择。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">3</span><span style="font-family: 宋体; color: #81888c; ">、把该异常转换成另一种异常。大多数情况下，这是指把一个低级的异常转换成应用级的异常（其含义更容易被用户了解的异常）。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">4</span><span style="font-family: 宋体; color: #81888c; ">、不要捕获异常。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　结论一：既然捕获了异常，就要对它进行适当的处理。不要捕获异常之后又把它丢弃，不予理睬。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　反例之二：不指定具体的异常</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　代码：</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">15</span><span style="font-family: 宋体; color: #81888c; ">行。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　许多时候人们会被这样一种</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8220;</span><span style="font-family: 宋体; color: #81888c; ">美妙的</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">&#8221;</span><span style="font-family: 宋体; color: #81888c; ">想法吸引：用一个</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句捕获所有的异常。最常见的情形就是使用</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch(Exception ex)</span><span style="font-family: 宋体; color: #81888c; ">语句。但实际上，在绝大多数情况下，这种做法不值得提倡。为什么呢？</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　要理解其原因，我们必须回顾一下</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句的用途。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句表示我们预期会出现某种异常，而且希望能够处理该异常。异常类的作用就是告诉</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Java</span><span style="font-family: 宋体; color: #81888c; ">编译器我们想要处理的是哪一种异常。由于绝大多数异常都直接或间接从</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">java.lang.Exception</span><span style="font-family: 宋体; color: #81888c; ">派生，</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch(Exception ex)</span><span style="font-family: 宋体; color: #81888c; ">就相当于说我们想要处理几乎所有的异常。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　再来看看前面的代码例子。我们真正想要捕获的异常是什么呢？最明显的一个是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">SQLException</span><span style="font-family: 宋体; color: #81888c; ">，这是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JDBC</span><span style="font-family: 宋体; color: #81888c; ">操作中常见的异常。另一个可能的异常是</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">IOException</span><span style="font-family: 宋体; color: #81888c; ">，因为它要操作</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">OutputStreamWriter</span><span style="font-family: 宋体; color: #81888c; ">。显然，在同一个</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">块中处理这两种截然不同的异常是不合适的。如果用两个</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">块分别捕获</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">SQLException</span><span style="font-family: 宋体; color: #81888c; ">和</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">IOException</span><span style="font-family: 宋体; color: #81888c; ">就要好多了。这就是说，</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">catch</span><span style="font-family: 宋体; color: #81888c; ">语句应当尽量指定具体的异常类型，而不应该指定涵盖范围太广的</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">Exception</span><span style="font-family: 宋体; color: #81888c; ">类。</span><span lang="EN-US" style="font-family: Arial; color: #81888c; "><br />
<br />
</span><span style="font-family: 宋体; color: #81888c; ">　　另一方面，除了这两个特定的异常，还有其他许多异常也可能出现。例如，如果由于某种原因，</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">executeQuery</span><span style="font-family: 宋体; color: #81888c; ">返回了</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">null</span><span style="font-family: 宋体; color: #81888c; ">，该怎么办？答案是让它们继续抛出，即不必捕获也不必处理。实际上，我们不能也不应该去捕获可能出现的所有异常，程序的其他地方还有捕获异常的机会</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">??</span><span style="font-family: 宋体; color: #81888c; ">直至最后由</span><span lang="EN-US" style="font-family: Arial; color: #81888c; ">JVM</span><span style="font-family: 宋体; color: #81888c; ">处理。</span></p>
</span>
<img src ="http://www.blogjava.net/ol-l/aggbug/266636.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ol-l/" target="_blank">ol_soft</a> 2009-04-20 23:50 <a href="http://www.blogjava.net/ol-l/archive/2009/04/20/266636.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK5.0新特性------泛型</title><link>http://www.blogjava.net/ol-l/archive/2009/04/20/266634.html</link><dc:creator>ol_soft</dc:creator><author>ol_soft</author><pubDate>Mon, 20 Apr 2009 15:49:00 GMT</pubDate><guid>http://www.blogjava.net/ol-l/archive/2009/04/20/266634.html</guid><wfw:comment>http://www.blogjava.net/ol-l/comments/266634.html</wfw:comment><comments>http://www.blogjava.net/ol-l/archive/2009/04/20/266634.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ol-l/comments/commentRss/266634.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ol-l/services/trackbacks/266634.html</trackback:ping><description><![CDATA[<span style="font-family: verdana; font-size: 12px; line-height: 18px; ">
<div><span style="font-family: 黑体; ">泛型是Java SE 1.5的新特性，泛型的本质是参数化类型，也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中，分别称为泛型类、泛型接口、泛型方法。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　Java语言引入泛型的好处是安全简单。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　在Java SE 1.5之前，没有泛型的情况的下，通过对类型Object的引用来实现参数的&#8220;任意化&#8221;，&#8220;任意化&#8221;带来的缺点是要做显式的强制类型转换，而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况，编译器可能不提示错误，在运行的时候才出现异常，这是一个安全隐患。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　泛型的好处是在编译的时候检查类型安全，并且所有的强制转换都是自动和隐式的，提高代码的重用率。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　泛型在使用中还有一些规则和限制：</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　1、泛型的类型参数只能是类类型（包括自定义类），不能是简单类型。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　2、同一种泛型可以对应多个版本（因为参数类型是不确定的），不同版本的泛型类实例是不兼容的。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　3、泛型的类型参数可以有多个。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　4、泛型的参数类型可以使用extends语句，例如&lt;T extends superclass>。习惯上成为&#8220;有界类型&#8221;。</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　5、泛型的参数类型还可以是通配符类型。例如Class&lt;?> classType = Class.forName(java.lang.String);</span></div>
<div><span style="font-family: 黑体; "><strong></strong></span></div>
<strong>
<div><span style="font-family: 黑体; ">　　泛型还有接口、方法等等，内容很多，需要花费一番功夫才能理解掌握并熟练应用。在此给出我曾经了解泛型时候写出的两个例子（根据看的印象写的），实现同样的功能，一个使用了泛型，一个没有使用，通过对比，可以很快学会泛型的应用，学会这个基本上学会了泛型70%的内容。</span></div>
<div><span style="font-family: 黑体; ">例子：</span></div>
<div>
<div class="highlighter">
<ol start="1" class="highlighter-j" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 35px; ">
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="keyword">package</span> gunct.jdk5;</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment">/**</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;b>Genericity。&lt;/b></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;p>&lt;b>详细说明：&lt;/b>&lt;/p></span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;!-- 在此添加详细说明 --></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * JDK5.0新特性，泛型。</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;p>&lt;b>修改列表：&lt;/b>&lt;/p></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;table width="100%" cellSpacing=1 cellPadding=3 border=1></span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;tr bgcolor="#CCCCFF">&lt;td>序号&lt;/td>&lt;td>作者&lt;/td>&lt;td>修改日期&lt;/td>&lt;td>修改内容&lt;/td>&lt;/tr></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;!-- 在此添加修改列表，参考第一行内容 --></span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;tr>&lt;td>1&lt;/td>&lt;td>Oliver&lt;/td>&lt;td>2008-12-18 下午01:45:51&lt;/td>&lt;td>建立类型&lt;/td>&lt;/tr></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * </span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * &lt;/table></span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * @version 1.0</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * @author Oliver</span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> * @since 1.0</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="comment"> */</span></li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; "><span class="keyword">public</span> <span class="keyword">class</span> Genericity&lt;T><span class="comment">//泛型T</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">{</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    <span class="keyword">private</span> T a;</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    <span class="keyword">public</span> <span class="keyword">void</span> setA(T a)</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    {</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        <span class="keyword">this</span>.a=a;</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    }</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    <span class="keyword">public</span> T getA()</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    {</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        <span class="keyword">return</span> a;</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    }</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    </li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(String[] args)</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    {</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        <span class="comment">//Integer类型</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        Genericity&lt;Integer> intVal=<span class="keyword">new</span> Genericity&lt;Integer>();</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        intVal.setA(<span class="keyword">new</span> Integer(<span class="number">5</span>));</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        System.out.println(intVal.getA());</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        <span class="comment">//String类型</span></li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        Genericity&lt;String> strVal= <span class="keyword">new</span> Genericity&lt;String>();</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        strVal.setA(<span class="string">"Gunct"</span>);</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">        System.out.println(strVal.getA());</li>
    <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">    }</li>
    <li class="alt" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style-type: decimal; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; ">}</li>
</ol>
</div>
</div>
</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></span>
<img src ="http://www.blogjava.net/ol-l/aggbug/266634.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ol-l/" target="_blank">ol_soft</a> 2009-04-20 23:49 <a href="http://www.blogjava.net/ol-l/archive/2009/04/20/266634.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中的transient，volatile和strictfp关键字</title><link>http://www.blogjava.net/ol-l/archive/2009/04/20/266632.html</link><dc:creator>ol_soft</dc:creator><author>ol_soft</author><pubDate>Mon, 20 Apr 2009 15:45:00 GMT</pubDate><guid>http://www.blogjava.net/ol-l/archive/2009/04/20/266632.html</guid><wfw:comment>http://www.blogjava.net/ol-l/comments/266632.html</wfw:comment><comments>http://www.blogjava.net/ol-l/archive/2009/04/20/266632.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ol-l/comments/commentRss/266632.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ol-l/services/trackbacks/266632.html</trackback:ping><description><![CDATA[<span  style="font-family: verdana; font-size: 12px; line-height: 18px; ">
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; ">ava中的transient，volatile和strictfp关键字&#160;<br />
&#160;&#160;&#160; 如果用transient声明一个实例变量，当对象存储时，它的值不需要维持。例如：&#160;</p>
<div class="dp-highlighter" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: #e7e5dc; width: 99%; overflow-x: auto; overflow-y: auto; margin-top: 18px !important; margin-right: 0px !important; margin-bottom: 18px !important; margin-left: 0px !important; padding-top: 1px; ">
<div class="bar" style="padding-left: 45px; ">
<div class="tools" style="padding-top: 3px; padding-right: 8px; padding-left: 10px; font: normal normal normal 9px/normal Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left-color: #6ce26c; ">Java代码&#160;<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=class%20T%20%7B%0A%20%20%20transient%20int%20a%3B%20%20%2F%2F%E4%B8%8D%E9%9C%80%E8%A6%81%E7%BB%B4%E6%8C%81%0A%20%20%20int%20b%3B%20%20%2F%2F%E9%9C%80%E8%A6%81%E7%BB%B4%E6%8C%81%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></div>
</div>
<ol class="dp-j" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; margin-left: 45px !important; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #5c5c5c; ">
    <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; "><span class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">class</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;T&#160;{&#160;&#160;</span></span></li>
    <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;&#160;&#160;<span class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">transient</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;</span><span class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">int</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;a;&#160;&#160;</span><span class="comment" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #008200; background-color: inherit; ">//不需要维持</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;&#160;</span></span></li>
    <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;&#160;&#160;<span class="keyword" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #006699; font-weight: bold; background-color: inherit; ">int</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;b;&#160;&#160;</span><span class="comment" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: #008200; background-color: inherit; ">//需要维持</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">&#160;&#160;</span></span></li>
    <li style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-width: initial; border-color: initial; list-style-type: decimal-leading-zero; list-style-image: initial; list-style-position: outside !important; border-left-width: 3px; border-left-style: solid; border-left-color: #6ce26c; background-color: #f8f8f8; color: #5c5c5c; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; line-height: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; color: black; background-color: inherit; ">}&#160;&#160;</span></li>
</ol>
</div>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><br />
&#160;&#160;&#160; 这里，如果T类的一个对象写入一个持久的存储区域，a的内容不被保存，但b的将被保存。&#160;<br />
&#160;&#160;&#160; volatile修饰符告诉编译器被volatile修饰的变量可以被程序的其他部分改变。在多线程程序中，有时两个或更多的线程共享一个相同的实例变量。考虑效率问题，每个线程可以自己保存该共享变量的私有拷贝。实际的变量副本在不同的时候更新，如当进入synchronized方法时。&#160;<br />
&#160;&#160;&#160; 用strictfp修饰类或方法，可以确保浮点运算（以及所有切断）正如早期的Java版本那样准确。切断只影响某些操作的指数。当一个类被strictfp修饰，所有的方法自动被strictfp修饰。&#160;<br />
&#160;&#160;&#160; strictfp的意思是FP-strict，也就是说精确浮点的意思。在Java虚拟机进行浮点运算时，如果没有指定strictfp关键字时，Java的编译器以及运行环境在对浮点运算的表达式是采取一种近似于我行我素的行为来完成这些操作，以致于得到的结果往往无法令你满意。而一旦使用了strictfp来声明一个类、接口或者方法时，那么所声明的范围内Java的编译器以及运行环境会完全依照浮点规范IEEE-754来执行。因此如果你想让你的浮点运算更加精确，而且不会因为不同的硬件平台所执行的结果不一致的话，那就请用关键字strictfp。&#160;<br />
你可以将一个类、接口以及方法声明为strictfp，但是不允许对接口中的方法以及构造函数声明strictfp关键字，例如下面的代码：&#160;<br />
<br />
1. 合法的使用关键字strictfp&#160;</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; ">
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">interface</span><span style="color: #000000; ">&#160;A&#160;{}&#160;&#160;<br />
</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&#160;FpDemo1&#160;{&#160;&#160;<br />
&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&#160;f()&#160;{}&#160;&#160;<br />
}</span></div>
</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; "><br />
2. 错误的使用方发</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 2em; line-height: 22px; font-size: 14px; ">
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">interface</span><span style="color: #000000; ">&#160;A&#160;{&#160;&#160;&#160;&#160;<br />
&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&#160;f();&#160;&#160;&#160;&#160;<br />
}&#160;&#160;&#160;&#160;<br />
&#160;&#160;&#160;&#160;<br />
</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&#160;</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&#160;FpDemo2&#160;{&#160;&#160;&#160;&#160;<br />
&#160;&#160;&#160;&#160;</span><span style="color: #0000FF; ">strictfp</span><span style="color: #000000; ">&#160;FpDemo2()&#160;{}&#160;&#160;&#160;&#160;<br />
}</span></div>
一旦使用了关键字strictfp来声明某个类、接口或者方法时，那么在这个关键字所声明的范围内所有浮点运算都是精确的，符合IEEE-754规范的。例如一个类被声明为strictfp，那么该类中所有的方法都是strictfp的。&#160;<br />
<br />
<br />
<br />
Keys: volatile&#160;<br />
<br />
使用对象：字段&#160;<br />
<br />
介绍：因为异步线程可以访问字段，所以有些优化操作是一定不能作用在字段上的。volatile有时&#160;<br />
<br />
可以代替synchronized。&#160;<br />
<br />
<br />
<br />
<br />
Keys：transient&#160;<br />
<br />
　　使用对象：字段&#160;<br />
<br />
　　介绍：字段不是对象持久状态的一部分，不应该把字段和对象一起串起。&#160;</p>
</span>
<img src ="http://www.blogjava.net/ol-l/aggbug/266632.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ol-l/" target="_blank">ol_soft</a> 2009-04-20 23:45 <a href="http://www.blogjava.net/ol-l/archive/2009/04/20/266632.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>