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. 

解决方案 »

  1.   

    不是,当a1<=0 时执行else
    assert (a1==0);
    System.out.println("Hello");
    这两句顺序执行,都会执行,相当于false;System……;
    不想输出的话用
    if(assert(a1==0))
    System.out.println("Hello");
      

  2.   

    请问老兄:
    在运行这个程序的时候会编译通过吗?
    assert(a1==0)这里有问题吧!
      

  3.   

    想结束程序的执行
    在assert (a1==0);后加上break;
      

  4.   

    to tianmiaohu(Tianmiao) : 谢谢~
    to whywzf(古风) : 不用if判断,用当assert后的逻辑表达式不成立时就会抛出异常
    to computerbird(↘ぷ赶赶ぷ↙)  : 可以编译通过,只要加上参数-source 1.4就可以了