成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Java keyword: assert

lyning / 2480人閱讀

The assert keyword is used in assert statement which is a feature of the Java programming language since Java 1.4. Assertion enables developers to test assumptions in their programs as a way to defect and fix bugs.

Syntax of assert statement
Syntax of an assert statement is as follow (short version):

assert expression1;

or (full version):

assert expression1 : expression2;

Where:
·expression1 must be a boolean expression.
·expression2 must return a value (must not return void).
The assert statement is working as follows:

  

If assertion is enabled, then the assert statement will be evaluated. Otherwise, it does not get executed.
If expression1 is evaluated to false, an AssertionError error is thrown which causes the program stops immediately. And depending on existence of expression2:
If expression2 does not exist, then the AssertionError is thrown with no detail error message:
If expression2 does exist, then a String representation of expression2’s return value is used as detail error message.
If expression1 is evaluate to true, then the program continues normally.

Enable assertion
By default, assertion is disabled at runtime. To enable assertion, specify the switch –enableassertions or -ea at command line of java program. For example, to enable assertion for the program called CarManager:

java –enableassertions CarManager

or this for short:

java –ea CarManager

Assertion can be enabled or disable specifically for named classes or packages. For more information on how to enable and disable assertion, go to: http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#enable-disable

Assertion examples
The following simple program illustrates the short version of assert statement:

public class AssertionExample {
public static void main(String[] args) {
// get a number in the first argument
int number = Integer.parseInt(args[0]);

    assert number <= 10; // stops if number > 10

    System.out.println("Pass");

}

}
When running the program above with this command:

java -ea AssertionExample 15

A java.lang.AssertionError error will be thrown:

Exception in thread "main" java.lang.AssertionError

   at AssertionExample.main(AssertionExample.java:6)

But the program will continue and print out “Pass” if we pass a number less than 10, in this command:

java -ea AssertionExample 8

And the following example is using the full version of assert statement:

public class AssertionExample2 {
public static void main(String[] args) {

    int argCount = args.length;

    assert argCount == 5 : "The number of arguments must be 5";

    System.out.println("OK");

}

}
When running the program above with this command:

java -ea AssertionExample2 1 2 3 4

it will throw this error:

Exception in thread "main" java.lang.AssertionError: The number of arguments must be 5

   at AssertionExample2.main(AssertionExample2.java:6)

Generally, assertion is enabled during development time to defect and fix bugs, and is disabled at deployment or production to increase performance.

出自CodeJava.Net

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/69775.html

相關(guān)文章

  • 使用duckie為JS做強(qiáng)類型檢查

    摘要:比如篇首這東西,使用聲明聲明一個(gè)類型跟聲明一個(gè)對(duì)象般簡(jiǎn)單,完全沒有學(xué)習(xí)成本??偨Y(jié)使用可以像聲明一個(gè)變量那樣聲明你的類型,且可以在運(yùn)行時(shí)進(jìn)行類型檢查。 JS是一門動(dòng)態(tài)類型語言,在定義一個(gè)函數(shù)時(shí),無法為其進(jìn)行強(qiáng)類型指定,如: function hello(name: string, content: string) { // 如果name或content不是string類型的話,就...

    tuomao 評(píng)論0 收藏0
  • Java關(guān)鍵字的筆記

    摘要:我在查詢一些資料的時(shí)候,發(fā)現(xiàn)資料中說的關(guān)鍵字都不一致,而且具體的單詞也都大不相同,所以我特意查閱了截止到目前最新的官方文檔,對(duì)此進(jìn)行了整理因?yàn)槭窃诘臅r(shí)候收購的公司,所以官網(wǎng)上我只找到了的文檔官方文檔鏈接中中的就是對(duì)應(yīng)的版本要把我在查詢一些資料的時(shí)候,發(fā)現(xiàn)資料中說的關(guān)鍵字都不一致,而且具體的單詞也都大不相同,所以我特意查閱了jdk6-15(截止到目前(2020.01.04)最新)的官方文檔,對(duì)...

    Tecode 評(píng)論0 收藏0
  • Java 關(guān)鍵字專題

    摘要:和也許看起來像是關(guān)鍵字,但是他們專門用于表示布爾類型的字面量。值得注意的是,在中整形值和布爾值之間不能相互轉(zhuǎn)換至少在語言層面。相關(guān)的操作等于不等于取反位與異或或條件與條件或三目運(yùn)算符在控制流程中使用一個(gè)布爾值可且僅可被轉(zhuǎn)型為,類型。 總覽 Java 語言中有 50 個(gè)關(guān)鍵字,這些關(guān)鍵字不能用作標(biāo)識(shí)符,如下圖所示(來自 jls8) showImg(https://segmentfault...

    Dogee 評(píng)論0 收藏0
  • JavaScript Testing

    Testing framework both use describe, it functions Jasmine(Behavior-Driven JavaScript) spyOn(User, save) jasmine.createSpy() the Jasmine framework has almost everything built into it including assertio...

    Eric 評(píng)論0 收藏0
  • 代碼測(cè)試:項(xiàng)目穩(wěn)健的有力保證

    摘要:懶惰,是促使人類科技發(fā)展的重要因素。就筆者了解,目前前端領(lǐng)域比較流行的單元測(cè)試框架有等等。。什么你想打開控制臺(tái)粘帖代碼執(zhí)行為了拯救你于無盡的加班測(cè)試中,是時(shí)候推薦你接入使用了。 懶惰,是促使人類科技發(fā)展的重要因素。我們告別刀耕火種的時(shí)代,正是因?yàn)槿藗儾粩嗟赝ㄟ^發(fā)明工具和優(yōu)化精簡(jiǎn)手動(dòng)的流程來實(shí)現(xiàn)效率的提升,讓人們能專注于自己專業(yè)的領(lǐng)域,其他的事情交給機(jī)械去處理。而同樣在前端的領(lǐng)域,我們也...

    ASCH 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<