要使assert起作用,要有-ea选项,对eclipse,可以这样设置http://www.vogella.com/blog/2009/02/06/enabling-java-assertions-per-default-in-eclipse-runtime-configuration/
一个简单测试
public class TestAssert { public static void main(String[] args) { assert false; System.out.println("test"); } }
输出:
Exception in thread "main" java.lang.AssertionError
at pack2.TestAssert.main(TestAssert.java:5)
(这里pack2是包名,忽略之)
又一个测试(to demonstrate the use of assert)
public class TestAssert { public static void main(String[] args) { assert false : "oops!"; System.out.println("test"); } }