public class AssertionExample {
public static void main(String[] args) {
int number = 5;
assert number > 0 : "Number should be positive";
System.out.println("Number is positive");
}
}
在上面的示例中,assert语句检查number是否大于0。如果number不大于0,则会抛出一个带有消息"Number should be positive"的AssertionError。如果条件为真,则程序将继续执行并打印"Number is positive"。