1. checked and unchecked exception

    checked 和 unchecked exception 的区别,资深一点的Java程序员都知道。这里我感兴趣的是该怎么用checked 和 unchecked exception.   最早的exception处理机制来自C++,在C中为了避免错误,我们会根据返回结果或者判断状态(文件操作)来保证程序的正确性。这样在代码中就会有大量的if/else判断最后甚至让读代码的人忘了程序片段的真正意图。因此C++中引入了exception的处理机制。不过C++为了兼容C,异常处理是选择性的。 Java是真正严格实现异常机制的语言。   从架构的角度什么时候用checked exception,什么时候用unchecked exception, 如何封装,处理,仁者见仁,智者见智。 有种选择是不用checked exception. Think In Java作者Bruce Eckel在Does Java need Checked Exceptions 提到这种设计选择。我的理解是原因有三点: 业务逻辑不愿意恢复的错误,比如Rod Johnson在J2EE Deveopment Without EJB中举了一个Service Locator 中JNDI Lookup failure时直接抛出Un-Checked exception。这样不会逼着系统处理这种异常,比如尝试恢复或者提示用户。 checked exception是编译时异常,强制要求处理,确实降低了程序出错的可能。但有时程序中还是大量充斥这种异常处理的代码。 程序员经常有意无意中丢掉了异常(简单的打印出异常信息,没有思考如何进一步处理),这是程序中引入bug的一种主要来源,并且比较难找。   据说Spring中DAO的封装中都是Runtime Exception. 不知是不是也是基于此考虑。

    2017/02/12 iteye

  2. closures, inner class and 函数指针

    一直不是很理解这三个感念之间的区别。Java是面向对象的编程语言,这点与现在流行的面向函数的动态编程语言不同。Javascript就是后者的一个例子。函数可以独立于对象存在,(或者说它本身就是一种对象)。   Java里没有函数指针,但可以向函数中传入一个匿名内部类模拟这种行为。   Java目前还未引入closures, 但基于JVM的Groovy已经有Closures概念。在Groovy中,所谓Closures就是一段大括号括起来的代码段。这段代码按照Java的习惯一般只能存在于函数中(初始化代码除外)并以函数名的方式调用。在Groovy中,这段代码可以赋给一个变量,通过这个变量来调用这段代码(很多时候放在大括号中直接匿名使用)。   那么代码段与Closures的区别是什么呢? 代码段一般是虚拟机碰到这段代码时就执行(方法调用,初始化,回调方法)。Closures只有在它的call()方法被调用时才执行。理解的不是很清楚,详细请参考:Closures Informal Guide   闭包有几个特点: 可以访问闭包范围外的变量(这点是不是跟Java中内部类相似?) 可以匿名用在任何允许用闭包的地方,比如Groovy的list的collect, each 调用中

    2017/02/12 iteye

  3. Oracle 笔记

    oracle Hints select  /*+ FIRST_ROWS(100) */  field1, field2,... from ... where ... 上面是Hints, 用于优化,告诉oracle 尽快返回前100行,适用于OLTP。还有ALL_ROWS,告诉Oracle用最少的资源处理所有行并一次返回。   FTS(全表扫描)可以并行,通过设置 DB_FILE_MULTIBLOCK_READ_COUNT 索引扫描不能并行,一般快些。 Oracle 提供了两种优化策略 一种是 Rule-base Optimizer    另一种是Cost-base Optimizer    

    2017/02/12 iteye

  4. 类的初始化

    类的初始化顺序: 静态初始化块 main函数中的输出(调用构造函数前) 实例的初始化块(初始化匿名内部类的地方) 构造函数初始化 import static java.lang.System.out; public class InitializerDemo { public InitializerDemo() { out.println("in constructor"); // Fourth } static { out.println("in static initializer"); // First } { out.println("in instance initializer"); // Third } public static void main(String[] args) { out.println("in main() method"); //second new InitializerDemo(); } }        

    2017/02/12 iteye

  5. AJAX学习笔记

    在用IFrame做Ajax时,需要考虑的有: 先定义隐藏的IFrame <iframe name="myIframe" src="blank.html" style="width:0px;height:0px" frameborder="0"> </iframe> 通过button,Href或者form提交,动态修改IFrame的src属性或者设置Href,Form的Target属性 如何在父窗口和IFrame页面传递数据 Three ways of Moving Data From an iframe to the Parent The iframe page can use it's own javascripts to transfer and place the data into the parent. <script> // - this is the script in the iframe results page var reps = document.getElementById('state_reps').innerHTML; parent.document.getElementById('results1').innerHTML = reps; </script>     The iframe page can pass its document object as an argument to a function in the parent, thereby allowing the parent to retrieve data from the iframe. <script> // - this is the script in parent page function showReps(doc){ var x = doc.getElementById('state_reps').innerHTML; document.getElementById('results2').innerHTML = x; } </script>     Place an onload event handler in the iframe tag in the parent document (this page). This method does not work with Netscape <iframe name="repIframe3" src="blank.html" onload="getIframeDoc()"></iframe> <script> function getIframeDoc(){ var iframeDoc = window.frames['repIframe3'].document; var sr = iframeDoc.getElementById('state_reps'); if (sr){ var reps3 = sr.innerHTML; document.getElementById('results3').innerHTML = reps3; } } </script>     <script> // - this is the script in the iframe results page parent.showReps(document); </script>

    2017/02/12 iteye

  6. Javascript 笔记

    正则表达式 //删除开头和结尾所有的空格 function trim(str) {   return str.replace(/(^\s+|\s+$)/g,''); } 注: ^ 开头 $ 结尾 \s 空格匹配 + 多次出现      g 全文匹配 i 忽略大小写   setTimeout(message, 3000);  3秒后执行函数message一次,如果要重复执行需要在message函数中再调用setTimeout一次    

    2017/02/12 iteye

  7. RESTful 笔记

    REST(Representational State Transfer)是 Roy Fielding 提出的一个描述互联系统架构风格的名词。   Another way to say REST is in HTTP, any request a client can make involves a URL and an HTTP method. With REST, the URL is designed to represent a noun and the HTTP method always maps to one of several standard verbs, which will be performed against that noun.In summary, each HTTP method will cause a well-defined action on the resource represented by the URL it operates on. The methods can be compared to SQL commands: GET is like "SELECT," DELETE is like "DELETE," POST is like "INSERT" with an auto-generated ID, and PUT is like "INSERT OR UPDATE IF EXISTS" with an ID specified.   REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful. stateless 有利于scalabilty,cluster 客户端可用cache提高性能   分层 这表示组件无法了解它与之交互的中间层以外的组件,这是博士论文中提到的,不是很理解   Get/Put/Delete方法的幂等性     Security 与SOAP架构相比,REST架构的优点是: SOAP接口隐藏在SOAP Envelope中了,对外暴露的是相同的URL(对于同一个web service), 不利于代理服务器根据HTTP method做安全控制, 除非解析SOAP Envelope 内容。   SOAP 采用HTTP Post也不利于浏览器做cache

    2017/02/12 iteye

  8. Date in Java

    以下两个是同一个时间: UTC, Coordinated Universal Time (UTC), use time zone of  GMT   GMT, Greenwich Mean Time   标准时间  18:00 GMT = 18:00+00:00 在用java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); 格式化时间时: Z表示的时间格式是+00:00 z表示的时间格式是UTC

    2017/02/12 iteye