自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JDK1.4特性assert詳解

開發(fā) 后端
JDK1.4特性assert,本文將就JDK1.4特性斷言向你介紹這個(gè)新特性的情況,希望通過此文你會(huì)清晰JDK1.4特性assert的情況。

JDK1.4中引入的一個(gè)新特性之一就是斷言(assert),為程序的調(diào)試提供了強(qiáng)有力的支持,以下的文檔根據(jù)SUNTEC內(nèi)容及相關(guān)內(nèi)容組成。

源代碼:

  1. /**  
  2. * Simple examples of the use of the new assertion feature in JDK1.4  
  3. *  
  4. * @author S.Ritter 16/7/2001  
  5. **/ 
  6. public class AssertExample {  
  7. public static void main(String[] args) {  
  8. int x = 10;  
  9. if (args.length > 0) {  
  10. try {  
  11. x = Integer.parseInt(args[0]);  
  12. catch (NumberFormatException nfe) {  
  13. /* Ignore */ 
  14. }  
  15. }  
  16. System.out.println("Testing assertion that x == 10");  
  17. assert x == 10:"Our assertion failed";  
  18. System.out.println("Test passed");  
  19. }  

由于引入了一個(gè)新的關(guān)鍵字,所以在編譯的時(shí)候就需要增加額外的參數(shù),要編譯成功,必須使用JDK1.4的javac并加上參數(shù)´-source 1.4´,例如可以使用以下的命令編譯上面的代碼:

javac -source 1.4 AssertExample.java

以上程序運(yùn)行使用斷言功能也需要使用額外的參數(shù)(并且需要一個(gè)數(shù)字的命令行參數(shù)),例如:

java -ea AssertExample 1

程序的輸出為:

  1. Testing assertion that x == 10 
  2. Exception in thread "main" java.lang.AssertionError: Our assertion failed  
  3. at AssertExample.main(AssertExample.java:20

由于輸入的參數(shù)不等于10,因此斷言功能使得程序運(yùn)行時(shí)拋出斷言錯(cuò)誤,注意是錯(cuò)誤,這意味著程序發(fā)生嚴(yán)重錯(cuò)誤并且將強(qiáng)制退出。斷言使用boolean值,如果其值不為true則拋出AssertionError并終止程序的運(yùn)行。

由于程序員的問題,斷言的使用可能會(huì)帶來副作用,例如:

  1. boolean isEnable=false;  
  2. //...  
  3. assert isEnable=true


這個(gè)斷言的副作用是因?yàn)樗薷某绦蜃兞康闹挡⑶覜]有拋出錯(cuò)誤,這樣的錯(cuò)誤如果不細(xì)心檢查很難發(fā)現(xiàn)。但是同時(shí)我們可以根據(jù)以上的副作用得到一個(gè)有用的特性,根據(jù)它測試是否將斷言打開了。

  1. /**  
  2. * Simple examples test enable assertion feature in JDK1.4  
  3. *  
  4. * @author Cherami 25/4/2002  
  5. **/ 
  6. public class AssertExample2 {  
  7. public static void main(String[] args) {  
  8. boolean assertEnable=false;  
  9. assert assertEnable=true;  
  10. if (assertEnable==false)  
  11. {  
  12. throw new RuntimeException("Assertions should be enable");  
  13. }  
  14. }  

如果我們不使用-ea參數(shù)運(yùn)行上面的程序,則控制臺(tái)將輸出:

  1. Exception in thread "main"   
  2. java.lang.RuntimeException: Assertions should be enable at AssertExample.main(AssertExample.java:14

JDK1.4特性assert詳解就介紹到這里,通過這個(gè)介紹希望你對JDK1.4特性assert有所了解。

【編輯推薦】

  1. JDK1.4在Windows下的環(huán)境配置
  2. JDK1.6在LINUX下的安裝配置
  3. JDK1.5中新的語言特征淺析
  4. JDK、SDK、JRE、JVM概念詳解
  5. JDK1.6的十大技術(shù)淺談
責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-07-09 09:09:46

JDK1.4

2009-07-07 11:17:14

JDK1.4環(huán)境配置

2009-07-09 11:21:08

JDK1.4安裝圖解

2009-02-01 10:10:00

Java資格認(rèn)證JDK1.4JDK1.6

2009-07-07 17:07:07

注冊表訪問JDK1.4

2009-08-04 08:55:54

Apache Wick

2024-01-26 08:33:14

JDK17JDK11版本

2020-09-02 14:08:04

Kotlin語言開發(fā)者

2009-07-09 14:57:08

JDK環(huán)境配置

2009-09-22 11:33:54

Java內(nèi)存模型

2010-09-25 09:30:28

JDK 7Java 7

2017-11-29 08:50:01

2021-05-06 20:03:00

JavaStream代碼

2011-07-29 09:31:32

JDK 7

2010-05-13 08:57:22

jQuery 1.4

2009-07-09 16:29:19

JDK版本

2011-05-20 09:43:23

JDK7

2009-07-09 10:28:19

線程池JDK5

2011-05-20 09:35:22

JDK7

2011-12-07 14:57:44

JavaNIO
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)