??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲人成人一区二区在线观看,亚洲国产精华液网站w,伊人春色在线http://www.aygfsteel.com/onewalkingman/category/52620.htmlzh-cnThu, 13 Sep 2012 17:36:19 GMTThu, 13 Sep 2012 17:36:19 GMT60Java~码易疏忽的十个问题Q{Q?/title><link>http://www.aygfsteel.com/onewalkingman/articles/387682.html</link><dc:creator>李敏</dc:creator><author>李敏</author><pubDate>Thu, 13 Sep 2012 15:59:00 GMT</pubDate><guid>http://www.aygfsteel.com/onewalkingman/articles/387682.html</guid><wfw:comment>http://www.aygfsteel.com/onewalkingman/comments/387682.html</wfw:comment><comments>http://www.aygfsteel.com/onewalkingman/articles/387682.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/onewalkingman/comments/commentRss/387682.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/onewalkingman/services/trackbacks/387682.html</trackback:ping><description><![CDATA[  在Java~码中,我们Ҏ(gu)犯一些错误,也容易疏忽一些问题,因此W者对日常~码中曾遇到的一些经典情形归Ux理成文,以共同探讨?br /><br /><br /> <h2>1. U结的同?/h2> <p><strong>现象</strong></p> <p>很多cȝ命名相同Q例如:常见于异常、常量、日志等c)Q导致在importӞ有时候张冠李_q种错误有时候很隐蔽。因为往往同名的类功能也类|所以IDE不会提示warn?/p> <p><strong>解决</strong></p> <p>写完代码Ӟ扫视下import部分Q看看有没有不熟(zhn)的。替换成正确导入后,要注意下注释是否也作相应修改?/p> <p><strong>启示</strong></p> <p>命名量避开重复名,特别要避开与JDK中的c重名,否则Ҏ(gu)导入错,同时存在大量重名c,在查找时Q也需要更多的辨别旉?br /><br /></p> <h2>2. 惛_然的API</h2> <p><strong>现象</strong></p> <p>有时候调用APIӞ会想当然的通过名字直接自信满满地调用,D很惊讶的一些错误:</p> <p>CZ一Qflag是trueQ?/p><pre>boolean flag = Boolean.getBoolean("true"); </pre> <p>可能老是false?/p> <p>CZ二:q是d的今天吗Q今q是2012q?span style="white-space: pre-wrap">Q不考虑闰年</span>Q?l果q是2012q_</p><pre>Calendar calendar = GregorianCalendar.getInstance(); calendar.roll(Calendar.DAY_OF_YEAR, -365); </pre> <p>下面的才是去q_</p><pre>calendar.add(Calendar.DAY_OF_YEAR, -365); </pre> <p><strong>解决办法</strong></p> <p>问自己几个问题,q个Ҏ(gu)我很熟?zhn)吗?有没有类似的API? 区别是什么?q例一而言Q需要区别的如下Q?/p><pre>Boolean.valueOf(b) VS Boolean.parseBoolean(b) VS Boolean.getBoolean(b); </pre> <p><strong>启示</strong></p> <p>名字L更详l点Q注释更清楚点,不要不经了解、测试就惛_然的用一些APIQ如果时间有限,用自己最为熟(zhn)的API?/p> <h2>3. 有时候溢出ƈ不难</h2> <p><strong>现象</strong></p> <p>有时候溢出ƈ不难Q虽然不常复玎ͼ</p> <p>CZ一Q?/p><pre>long x=Integer.MAX_VALUE+1; System.out.println(x); </pre> <p>x是多?竟然?2147483648Q明明加?之后q是long的范围。类似的l常出现在时间计:</p><pre><code>数字1×数字2×数字3… </code></pre> <p>CZ二:</p> <p>在检查是否ؓ正数的参数校验中Qؓ了避免重载,选用参数number, 于是下面代码l果于0Q也是因为溢出导_</p><pre>Number i=Long.MAX_VALUE; System.out.println(i.intValue()>0); </pre> <p><strong>解决</strong></p> <ol><li>让第一个操作数是long型,例如加上L或者lQ不写字母lQ因为和数字1太相gQ;</li><li>不确定时Q还是用重载吧Q即使用doubleValue()Q当参数是BigDecimal参数Ӟ也不能解决问题?/li></ol> <p><strong>启示</strong></p> <p>Ҏ(gu)字运用要保持敏感Q涉及数字计就要考虑溢出Q涉及除法就要考虑被除数是0Q实在容U不下了可以考虑B(ti)igDecimal之类?/p> <h2>4. 日志跑哪了?</h2> <p><strong>现象</strong></p> <p>有时候觉得log都打了,怎么找不刎ͼ</p> <p>CZ一Q没有stack traceQ?/p><pre> } catch (Exception ex) { log.error(ex); } </pre> <p>CZ二:找不到l(f)ogQ?/p><pre>} catch (ConfigurationException e) { e.printStackTrace(); } </pre> <p><strong>解决</strong></p> <ol><li>替换成log.error(ex.getMessage(),ex);</li><li>换成普通的log4j吧,而不是System.out?/li></ol> <p><strong>启示</strong></p> <ol><li>API定义应该避免让h犯错Q如果多加个重蝲的log.error(Exception)自然没有错误发生</li><li>在品代码中Q用的一些方法要考虑是否有效Q用e.printStackTrace()要想下终?Console)在哪?/li></ol> <h2>5. 遗忘的volatile</h2> <p><strong>现象</strong></p> <p>在DCL模式中,L忘记加一个Volatile?/p><pre>private static CacheImpl instance; //lose volatile public static CacheImpl getInstance() { if (instance == null) { synchronized (CacheImpl.class) { if (instance == null) { instance = new CacheImpl (); } } } return instance; } </pre> <p><strong>解决</strong></p> <p>毋庸|疑Q加上一个吧Qsynchronized 锁的是一块代码(整个Ҏ(gu)或某个代码块Q,保证的是q?#8221;?#8220;代码的可见性及原子性,但是instance == nullW一ơ判断时不再范围内的。所以可能读出的是过期的null?/p> <p><strong>启示</strong></p> <p>我们L觉得某些低概率的事g很难发生Q例如某个时间ƈ发的可能性、某个异常抛出的可能性,所以不加控Ӟ但是如果可以Q还是按照前人的“最佛_?#8221;来写代码吧。至不用过多解释ؓ啥另辟蹊径?/p> <h2>6. 不要影响彼此</h2> <p><strong>现象</strong></p> <p>在释攑֤个IO资源Ӟ都会抛出IOException Q于是可能ؓ了省事如此写Q?/p><pre>public static void inputToOutput(InputStream is, OutputStream os, boolean isClose) throws IOException { BufferedInputStream bis = new BufferedInputStream(is, 1024); BufferedOutputStream bos = new BufferedOutputStream(os, 1024); …. if (isClose) { bos.close(); bis.close(); } } </pre> <p>假设bos关闭p|Qbisq能关闭吗?当然不能Q?/p> <p><strong>解决办法</strong></p> <p>虽然抛出的是同一个异常,但是q是各自捕获各的为好。否则第一个失败,后一个面没有机会去释放资源了?/p> <p><strong>启示</strong></p> <p>代码/模块之间可能存在依赖Q要充分识别对相互的依赖?/p> <h2>7. 用断a取代参数校验</h2> <p><strong>现象</strong></p> <p>如题所提,作ؓ防M式编E常用的方式Q断aQ写在品代码中做参数校验等。例如:</p><pre>private void send(List< Event> eventList) { assert eventList != null; } </pre> <p><strong>解决</strong></p> <p>换成正常的统一的参数校验方法。因为断a默认是关闭的Q所以v不v作用完全在于配置Q如果采用默认配|,l历了eventList != nulll果q没有vC用,徒劳无功?/p> <p><strong>启示</strong></p> <p>有的时候,代码起不起作用,不仅在于用例Q还在于配置Q例如断a是否启用、logU别{,要结合真实环境做有用~码?/p> <h2>8. 用户认知负担有时候很?/h2> <p><strong>现象</strong></p> <p>先来比较三组例子Q看看那些看着更顺畅?</p> <p>CZ一Q?/p><pre>public void caller(int a, String b, float c, String d) { methodOne(d, z, b); methodTwo(b, c, d); } public void methodOne(String d, float z, String b) public void methodTwo(String b, float c, String d) </pre> <p>CZ二:</p><pre>public boolean remove(String key, long timeout) { Future< Boolean> future = memcachedClient.delete(key); public boolean delete(String key, long timeout) { Future< Boolean> future = memcachedClient.delete(key); </pre> <p>CZ三:</p><pre>public static String getDigest(String filePath, DigestAlgorithm algorithm) public static String getDigest(String filePath, DigestAlgorithm digestAlgorithm) </pre> <p><strong>解决</strong></p> <ol><li>保持参数传递顺序;</li><li>remove变成了deleteQ显得突兀了点Q?l一表达更好Q?/li><li>保持表达Q少~写也会看v来流畅点?/li></ol> <p><strong>启示</strong></p> <p>在编码过E中Q不是参数的顺序还是命名都量l一Q这LL认知负担会很,不要要用户容易犯错或qh。例如用枚D代替string从而不让用戯惑到底传什么string, 诸如此类?/p> <h2>9. 忽视日志记录时机、?/h2> <p><strong>现象</strong></p> <p>存在下面两则CZQ?/p> <p>CZ一Q该不该记录日志Q?/p><pre>catch (SocketException e) { LOG.error("server error", e); throw new ConnectionException(e.getMessage(), e); } </pre> <p>CZ二:C么别日志?</p> <p>在用L录系l中Q每ơ失败登录:</p><pre>LOG.warn("Failed to login by "+username+"); </pre> <p><strong>解决</strong></p> <ol><li>U除日志记录Q在遇到需要re-throw的异常时Q如果每个h都按照先记录后throw的方式去处理Q那么对一个错误会记录太多的日志,所以不推荐如此做;但是如果re-throw出去的exception没有带完整的trace( 即cause)Q那么最好还是记录下?/li><li>如果恶意dQ那pȝ内部会出现太多WARNQ从而让理员误以ؓ是代码错误。可以反馈用户以错误Q但是不要记录用户错误的行ؓQ除非想辑ֈ控制的目的?/li></ol> <p><strong>启示</strong></p> <p>日志改不改记Q记成什么别?如何讎ͼq些都是问题Q一定要Ҏ(gu)具体情况Q需要考虑Q?/p> <ol><li>是用戯为错误还是代码错误?</li><li>记录下来的日志,能否能给别h在不造成q多的干扰前提下提供有用的信息以快速定位问题?/li></ol> <h2>10. 忘设初始定w</h2> <p><strong>现象</strong></p> <p>在JAVA中,我们常用Collection中的Map做Cache,但是我们l常会遗忘设|初始容量?/p><pre>cache = new LRULinkedHashMap< K, V>(maxCapacity); </pre> <p><strong>解决</strong></p> <p>初始定w的媄响有多大Q拿LinkedHashMap来说Q初始容量如果不讄默认?6Q超q?6×LOAD_FACTOR,会resize(2 * table.length),扩大2倍:采用 Entry[] newTable = new Entry[newCapacity]; transfer(newTable)Q即整个数组CopyQ?那么对于一个需要做大容量CACHE来说Q从16变成一个很大的数量Q需要做多少ơ数l复制可惌知。如果初始容量就讄很大Q自然会减少resize, 不过可能会担心,初始定w讄很大Ӟ没有Cache内容仍然会占用过大体U。其实可以参考以下表格简单计下, 初始时还没有cache内容, 每个对象仅仅?字节引用而已?/p> <ul><li>memory for reference fields (4 bytes each);</li><li>memory for primitive fields</li></ul> <p> <table border="1"> <tbody> <tr> <td>Java type</td> <td>Bytes required</td></tr> <tr> <td>boolean</td> <td>1</td></tr> <tr> <td>byte</td> <td> </td></tr> <tr> <td>char</td> <td>2</td></tr> <tr> <td>short</td> <td> </td></tr> <tr> <td>int</td> <td>4</td></tr> <tr> <td>float</td> <td> </td></tr> <tr> <td>long</td> <td>8</td></tr> <tr> <td>double</td> <td> </td></tr></tbody></table></p> <p><strong>启示</strong></p> <p>不仅是map, q有stringBuffer{,都有定wresize的过E,如果数据量很大,׃能忽视初始容量可以考虑讄下,否则不仅有频J的 resizeq容易浪费容量?/p> <p>在Java~程中,除了上面枚D的一些容易忽视的问题Q日常实践中q存在很多。相信通过不断的ȝ和努力,可以我们的E序完美呈现l读者?/p><br /> <p><br />原文Q?br /><font face="Verdana"><span style="color: red">http://www.infoq.com/cn/articles/10-java-questions-easy-to-ignore</span></font></p><img src ="http://www.aygfsteel.com/onewalkingman/aggbug/387682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/onewalkingman/" target="_blank">李敏</a> 2012-09-13 23:59 <a href="http://www.aygfsteel.com/onewalkingman/articles/387682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank">ľ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ԭ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɾ</a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ԫ</a>| <a href="http://" target="_blank">⿪</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ר</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">뵺</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">⿪</a>| <a href="http://" target="_blank">կ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">÷</a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ij</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͬ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˹</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>