assert
是在
J2SE1.4
中引入的新特性,
assertion
就是在代碼中包括的布爾型狀態(tài),程序員認(rèn)為這個(gè)狀態(tài)是
true
。一般來(lái)說(shuō)
assert
在開(kāi)發(fā)的時(shí)候是檢查程序的安全性的,在發(fā)布的時(shí)候通常都不使用
assert
。在
1.4
中添加了
assert
關(guān)鍵字和
java.lang.AssertError
類(lèi)的支持。
?????
首先,我們有必要從一個(gè)例子說(shuō)起
assert
public class AssertTest
{
?public static void main(String[] args)
?{
? AssertTest at = new AssertTest();
? at.assertMe(true);
? at.assertMe(false);
??
?}
?
?private? void assertMe(boolean boo)
?{
? assert boo?true:false;
? System.out.println(true condition);
?}
?
}
程序中包含了
assert
的話,你要用
javac -source 1.4 xxx.java
來(lái)編譯,否則編譯器會(huì)報(bào)錯(cuò)的。要想讓
assert
得部分運(yùn)行的話,要使用
java -ea xxx
來(lái)運(yùn)行,否則包含
assert
得行會(huì)被忽略。下面我們運(yùn)行
javac -source 1.4 AssertTest.java
java -ea AssertTest
看看結(jié)果的輸出是:
true condition
Exception in thread main java.lang.AssertionError
??????? at AssertTest.assertMe(AssertTest.java:13)
??????? at AssertTest.main(AssertTest.java:7)
當(dāng)我們運(yùn)行
at.assertMe(true)
得時(shí)候,由于
assert boo?true:false
相當(dāng)于
assert true;
因此沒(méi)有任何問(wèn)題,程序往下執(zhí)行打印出
true condition
,但是執(zhí)行
at.assertMe(false)
的時(shí)候相當(dāng)于
assert false
,這個(gè)時(shí)候解釋器就會(huì)拋出
AssertionError
了,程序就終止了。大家必須清楚
AssertionError
是繼承自
Error
得,因此你可以不再程序中
catch
它的,當(dāng)然你也可以在程序中
catch
它然后程序可以繼續(xù)執(zhí)行。例如:
public class AssertTest
{
?public static void main(String[] args)
?{
? AssertTest at = new AssertTest();
? try
? {
?? at.assertMe(true);
?? at.assertMe(false);
? }
? catch(AssertionError ae)
? {
?? System.out.println(AsseriontError catched);
? }
? System.out.println(go on);
??
?}
?
?private? void assertMe(boolean boo)
?{
? assert boo?true:false;
? System.out.println(true condition);
?}
?
}
??? assert
還有另外一種表達(dá)的方式,就是
assert exp1:exp2;
其中
exp1
是個(gè)
boolean
返回值得表達(dá)式,而
exp2
可以是原始的數(shù)據(jù)類(lèi)型或者對(duì)象都可以例如:
?? boolean boo = true;
?? String str = null;
??? assert boo = false
:
str=error;
我們剛開(kāi)始講得
assert exp1
得形式,當(dāng)
exp1
是
false
得時(shí)候,
AssertionError
得默認(rèn)構(gòu)造器會(huì)被調(diào)用,但是
assert exp1:exp2
這樣的形式,當(dāng)
exp1
為
true
的時(shí)候后面
exp2
被或略,如果
false
的話,后面的表達(dá)式的結(jié)果會(huì)被計(jì)算出來(lái)并作為
AssertionError
得構(gòu)造器參數(shù)??聪旅娴睦樱?/span>
public class AssertTest
{
?public static void main(String[] args)
?{
? AssertTest at = new AssertTest();
? at.assertMe(true);
? at.assertMe(false);
??
?}
?
?private? void assertMe(boolean boo)
?{
? String s = null;
? assert boo?true:false:s = hello world;
? System.out.println(true condition);
?}
?
}
運(yùn)行的時(shí)候會(huì)得到這樣的結(jié)果
true condition
Exception in thread main java.lang.AssertionError: hello world
??????? at AssertTest.assertMe(AssertTest.java:14)
??????? at AssertTest.main(AssertTest.java:7)
Assert
最好不要濫用,原因是
assert
并不一定都是
enable
的,下面兩種情況就不應(yīng)該用
assert
1
不要再
public
的方法里面檢查參數(shù)是不是為
null
之類(lèi)的操作
例如
public int get(String s)
?? {
?????? assert s != null;
?? }
如果需要檢查也最好通過(guò)
if s = null
拋出
NullPointerException
來(lái)檢查
2
不要用
assert
來(lái)檢查方法操作的返回值來(lái)判斷方法操作的結(jié)果
??
例如
assert list.removeAll();
這樣看起來(lái)好像沒(méi)有問(wèn)題
但是想想如果
assert
被
disable
呢,那樣他就不會(huì)被執(zhí)行了
所以
removeAll()
操作就沒(méi)有被執(zhí)行
?
可以這樣代替
boolean boo = list.removeAl();
assert boo;