By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertionsTo enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch. You specify the granularity with the arguments that you provide to the switch: no arguments
Enables or disables assertions in all classes except system classes.
packageName...
Enables or disables assertions in the named package and any subpackages.
...
Enables or disables assertions in the unnamed package in the current working directory.
className
Enables or disables assertions in the named class
For example, the following command runs a program, BatTutor, with assertions enabled in only package com.wombat.fruitbat and its subpackages: java -ea:com.wombat.fruitbat... BatTutor
If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. For example, the following command runs the BatTutor program with assertions enabled in package com.wombat.fruitbat but disabled in class com.wombat.fruitbat.Brickbat: java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat BatTutor
The above switches apply to all class loaders. With one exception, they also apply to system classes (which do not have an explicit class loader). The exception concerns the switches with no arguments, which (as indicated above) do not apply to system classes. This behavior makes it easy to enable asserts in all classes except for system classes, which is commonly desirable. To enable assertions in all system classes, use a different switch: -enablesystemassertions, or -esa. Similarly, to disable assertions in system classes, use -disablesystemassertions, or -dsa. For example, the following command runs the BatTutor program with assertions enabled in system classes, as well as in the com.wombat.fruitbat package and its subpackages: java -esa -ea:com.wombat.fruitbat...
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains a circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class. This topic is discussed in detail in the assertions specification.
Enables or disables assertions in all classes except system classes.
packageName...
Enables or disables assertions in the named package and any subpackages.
...
Enables or disables assertions in the unnamed package in the current working directory.
className
Enables or disables assertions in the named class
For example, the following command runs a program, BatTutor, with assertions enabled in only package com.wombat.fruitbat and its subpackages: java -ea:com.wombat.fruitbat... BatTutor
If a single command line contains multiple instances of these switches, they are processed in order before loading any classes. For example, the following command runs the BatTutor program with assertions enabled in package com.wombat.fruitbat but disabled in class com.wombat.fruitbat.Brickbat: java -ea:com.wombat.fruitbat... -da:com.wombat.fruitbat.Brickbat BatTutor
The above switches apply to all class loaders. With one exception, they also apply to system classes (which do not have an explicit class loader). The exception concerns the switches with no arguments, which (as indicated above) do not apply to system classes. This behavior makes it easy to enable asserts in all classes except for system classes, which is commonly desirable. To enable assertions in all system classes, use a different switch: -enablesystemassertions, or -esa. Similarly, to disable assertions in system classes, use -disablesystemassertions, or -dsa. For example, the following command runs the BatTutor program with assertions enabled in system classes, as well as in the com.wombat.fruitbat package and its subpackages: java -esa -ea:com.wombat.fruitbat...
The assertion status of a class (enabled or disabled) is set at the time it is initialized, and does not change. There is, however, one corner case that demands special treatment. It is possible, though generally not desirable, to execute methods or constructors prior to initialization. This can happen when a class hierarchy contains a circularity in its static initialization. If an assert statement executes before its class is initialized, the execution must behave as if assertions were enabled in the class. This topic is discussed in detail in the assertions specification.
解决方案 »
- 有谁用swing去访问过jboss中的数据源啊!请教。。
- 关于int,Integer的问题?
- 招聘java开发工程师
- 有没有办法可以测网页上applet的运行时间?
- 500分求教:国外软件源码直接修改.java中字符串汉化编译出来是乱码,试了n种方法还是不行,500分求教各位达人
- 请问(string),tostring还有string.valueof()这几个方法有什么差别啊
- 怎么用Choice?
- 关闭窗口的这两种方法有什么区别吗?
- utton.addActionListener(new my());//为什么想在打开一个同样的东西不行呢?
- 请问在 JTable 中,如何将焦点总是指向最后一行???
- 请教(在线等待)
- 关于继承和构造函数的问题
assert (a1==0);
System.out.println("Hello");
这两句顺序执行,都会执行,相当于false;System……;
不想输出的话用
if(assert(a1==0))
System.out.println("Hello");
在运行这个程序的时候会编译通过吗?
assert(a1==0)这里有问题吧!
在assert (a1==0);后加上break;
to whywzf(古风) : 不用if判断,用当assert后的逻辑表达式不成立时就会抛出异常
to computerbird(↘ぷ赶赶ぷ↙) : 可以编译通过,只要加上参数-source 1.4就可以了