资讯专栏INFORMATION COLUMN

Java keyword: assert

lyning / 2271人阅读

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69775.html

相关文章

  • 使用duckie为JS做强类型检查

    摘要:比如篇首这东西,使用声明声明一个类型跟声明一个对象般简单,完全没有学习成本。总结使用可以像声明一个变量那样声明你的类型,且可以在运行时进行类型检查。 JS是一门动态类型语言,在定义一个函数时,无法为其进行强类型指定,如: function hello(name: string, content: string) { // 如果name或content不是string类型的话,就...

    tuomao 评论0 收藏0
  • Java关键字的笔记

    摘要:我在查询一些资料的时候,发现资料中说的关键字都不一致,而且具体的单词也都大不相同,所以我特意查阅了截止到目前最新的官方文档,对此进行了整理因为是在的时候收购的公司,所以官网上我只找到了的文档官方文档链接中中的就是对应的版本要把我在查询一些资料的时候,发现资料中说的关键字都不一致,而且具体的单词也都大不相同,所以我特意查阅了jdk6-15(截止到目前(2020.01.04)最新)的官方文档,对...

    Tecode 评论0 收藏0
  • Java 关键字专题

    摘要:和也许看起来像是关键字,但是他们专门用于表示布尔类型的字面量。值得注意的是,在中整形值和布尔值之间不能相互转换至少在语言层面。相关的操作等于不等于取反位与异或或条件与条件或三目运算符在控制流程中使用一个布尔值可且仅可被转型为,类型。 总览 Java 语言中有 50 个关键字,这些关键字不能用作标识符,如下图所示(来自 jls8) showImg(https://segmentfault...

    Dogee 评论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 评论0 收藏0
  • 代码测试:项目稳健的有力保证

    摘要:懒惰,是促使人类科技发展的重要因素。就笔者了解,目前前端领域比较流行的单元测试框架有等等。。什么你想打开控制台粘帖代码执行为了拯救你于无尽的加班测试中,是时候推荐你接入使用了。 懒惰,是促使人类科技发展的重要因素。我们告别刀耕火种的时代,正是因为人们不断地通过发明工具和优化精简手动的流程来实现效率的提升,让人们能专注于自己专业的领域,其他的事情交给机械去处理。而同样在前端的领域,我们也...

    ASCH 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<