我的一畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            2 隨筆 :: 14 文章 :: 3 評論 :: 0 Trackbacks

          assert 是在 J2SE1.4 中引入的新特性, assertion 就是在代碼中包括的布爾型狀態,程序員認為這個狀態是 true 。一般來說 assert 在開發的時候是檢查程序的安全性的,在發布的時候通常都不使用 assert 。在 1.4 中添加了 assert 關鍵字和 java.lang.AssertError 類的支持。

          ????? 首先,我們有必要從一個例子說起 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 來編譯,否則編譯器會報錯的。要想讓 assert 得部分運行的話,要使用 java -ea xxx 來運行,否則包含 assert 得行會被忽略。下面我們運行

          javac -source 1.4 AssertTest.java

          java -ea AssertTest

          看看結果的輸出是:

          true condition

          Exception in thread main java.lang.AssertionError

          ??????? at AssertTest.assertMe(AssertTest.java:13)

          ??????? at AssertTest.main(AssertTest.java:7)

          當我們運行 at.assertMe(true) 得時候,由于 assert boo?true:false 相當于 assert true; 因此沒有任何問題,程序往下執行打印出 true condition ,但是執行 at.assertMe(false) 的時候相當于 assert false ,這個時候解釋器就會拋出 AssertionError 了,程序就終止了。大家必須清楚 AssertionError 是繼承自 Error 得,因此你可以不再程序中 catch 它的,當然你也可以在程序中 catch 它然后程序可以繼續執行。例如:

          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 還有另外一種表達的方式,就是 assert exp1:exp2; 其中 exp1 是個 boolean 返回值得表達式,而 exp2 可以是原始的數據類型或者對象都可以例如:

          ?? boolean boo = true;

          ?? String str = null;

          ??? assert boo = false str=error;

          我們剛開始講得 assert exp1 得形式,當 exp1 false 得時候, AssertionError 得默認構造器會被調用,但是 assert exp1:exp2 這樣的形式,當 exp1 true 的時候后面 exp2 被或略,如果 false 的話,后面的表達式的結果會被計算出來并作為 AssertionError 得構造器參數。看下面的例子:

          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);

          ?}

          ?

          } 運行的時候會得到這樣的結果

          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 的,下面兩種情況就不應該用 assert

          1 不要再 public 的方法里面檢查參數是不是為 null 之類的操作

          例如 public int get(String s)

          ?? {

          ?????? assert s != null;

          ?? }

          如果需要檢查也最好通過 if s = null 拋出 NullPointerException 來檢查

          2 不要用 assert 來檢查方法操作的返回值來判斷方法操作的結果 ??

          例如 assert list.removeAll(); 這樣看起來好像沒有問題 但是想想如果 assert disable 呢,那樣他就不會被執行了 所以 removeAll() 操作就沒有被執行 ? 可以這樣代替
          boolean boo = list.removeAl();

          assert boo;

          posted on 2006-09-27 10:48 王某某 閱讀(14010) 評論(1)  編輯  收藏 所屬分類: Java基礎

          評論

          # re: java assert的用法[轉載] 2006-09-27 10:58 王某某
          對于非公有的方法,通常應該使用assertion來檢查他們的參數,而不使用正常的檢查語句。  回復  更多評論
            

          主站蜘蛛池模板: 马边| 涪陵区| 鄂尔多斯市| 邮箱| 确山县| 北川| 迭部县| 铁力市| 贵定县| 宁蒗| 宁国市| 长乐市| 十堰市| 丰城市| 河源市| 防城港市| 宽城| 丹巴县| 荣昌县| 礼泉县| 河源市| 大丰市| 邢台市| 新兴县| 明水县| 巴青县| 来安县| 弥渡县| 木兰县| 黎平县| 固阳县| 涪陵区| 买车| 马山县| 黔南| 阜平县| 英吉沙县| 宁安市| 无棣县| 安陆市| 襄城县|