assert語句主要是用在調試時,在默認情况下是關閉的。
可以用 -ea 或 -da 在終端行起用或關閉斷言語句。
看看如下的代碼運行結果便可知。

1 public class Test
2 {
3 public static void main(String[] args)
4 {
5 double x = -6;
6 assert x > 0: "x must be positive.";
7 double y = Math.sqrt(x);
8 System.out.println(y);
9 }
10 }