Assert 在Java和Groovy中的区别
对于防御性编程而言,assert一直都是非常重要的工具。但对于熟悉-ea参数的Java程序员而言,当他们看到所有Groovy断言无论是否有-ea都照样执行时一定会非常困惑。
事实上,Groovy的断言是无条件开启的!
对此,开发人员Roshan Dawrani做了如下解释:(原文)
The JDK documentation has a long chapter on assertions that talks about the disabling feature for assertions and its impact on compiling, starting the VM, and resulting design issues. Although this is fine and the design rationale behind Java assertions is clear, we feel the disabling feature is the
biggest stumbling block for using assertions in Java. You can never be sure that your assertions are really executed.
在Core Java(或者是TIJ?记不清了)里,作者提到assertion类似“在海边开赛艇要带好你的卡通救生圈;如果船已经在大海中央,就把救生圈扔掉好了”。不过,在Groovy中,显然这种防御措施会一直跟随你到大海的任意角落(希望不是海底)。
PS:如果仔细看看Groovy断言的输出,你一定会意识到它对于debug是多么有用:
1: def xml = new XmlParser().parseText("x")
2: assert "y" == xml.text()
3: -----
4: output:
5:
6: assert "y" == xml.text()
7: | | |
8: | | x
9: | test[attributes={}; value=[x]]
10: false
11:
******
Quote of the day:
I didn't really say everything I said. - Yogi Berra
相关文档:
先来了解一下链表模式的原理:
首先写一个JavaBean,内容是要添加的元素和该元素的节点。
public class NodeBean implements Serializable
{
private Object data; //元素本身
private NodeBean next; //下一个节点
&n ......
时间过的真快,不知不觉来到传智播客已经一个多月了,这一个月主要学习了Java基础部分,虽然每天都是敲代码到晚上1、2点才睡觉,但是还是感觉时间不够用,因为需要学习巩固的东西太多太多了,总感觉掌握的不够好,这一个月过的很累但也很充实。
这几天开始 ......
java工作者社区主办
1. 【复选】收入有涨,有干头
22.22%
2. 【复选】收入不乐观,没干头
8.89%
3. 【单选】年收入50万以上
2.22%
4. 【单选】年收入20万~50万
4.44%
5. 【单选】年收入10万~20万 ......
首先,要下载一个连接mysql数据库的驱动程序: mysql-connector-java-3.0.15-ga-bin.jar, 这个驱动程序不需要做其它配置,也就是说,对于mysql数据库,不必像access或者oracle要建立odbc数据源。
其次,将上面的.jar文件加入到classpath环境变量中。
最后,就是写代码进行测试了。
主要代码如下:
......