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ù)。看下面的例子:

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;