大哥们,帮我看看这个程序吧~~~~~~~~~

CTAer 2003-10-20 10:28:07
/**
本程序对断定进行测试
*/
public class Test11
{
public static void main(String[] args)
{
System.out.println("Testing ...");
int a1;
a1=-10;
if (a1>0)
{
}
else
{
assert (a1==0);
System.out.println("Hello");
}
}
}
以上是SL-275教程中的一个例子,我将a1赋值为-10,在assert(a1==0)处是否就应该结束执行,为什么还是输出"Hello"呢?
谢谢~
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
CTAer 2003-10-21
  • 打赏
  • 举报
回复
to tianmiaohu(Tianmiao) : 谢谢~
to whywzf(古风) : 不用if判断,用当assert后的逻辑表达式不成立时就会抛出异常
to computerbird(↘ぷ赶赶ぷ↙) : 可以编译通过,只要加上参数-source 1.4就可以了
fft123 2003-10-21
  • 打赏
  • 举报
回复
想结束程序的执行
在assert (a1==0);后加上break;
computerbird 2003-10-20
  • 打赏
  • 举报
回复
请问老兄:
在运行这个程序的时候会编译通过吗?
assert(a1==0)这里有问题吧!
whywzf 2003-10-20
  • 打赏
  • 举报
回复
不是,当a1<=0 时执行else
assert (a1==0);
System.out.println("Hello");
这两句顺序执行,都会执行,相当于false;System……;
不想输出的话用
if(assert(a1==0))
System.out.println("Hello");
tianmiaohu 2003-10-20
  • 打赏
  • 举报
回复
By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions

To 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.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧