1.eclipse、myeclipse开启assert(断言),默认是关闭,如下:

说白了就是设置一下jvm的参数,参数是-ea或者-enableassertions

assert的使用

 

2.assert格式

(1)assert [boolean 表达式]

如果[boolean表达式]为true,则程序继续执行。
如果为false,则程序抛出AssertionError,并终止执行。
(2)assert[boolean 表达式 : 错误表达式 (日志)]
如果[boolean表达式]为true,则程序继续执行。
如果为false,则程序抛出java.lang.AssertionError,输出[错误信息]。
 

 

备注:assert boolean表达式如果是false会造成如下问题:
 assert的使用

 

 
3.例子,如下:

  public static void main(String[] args) {
    String s = null;
    assert s!=null?true:false;
    assert false;
    System.out.println("end");
  }

 

相关文章:

  • 2022-01-09
  • 2021-12-13
  • 2022-01-04
  • 2021-07-07
  • 2021-12-14
  • 2022-02-27
  • 2021-12-19
猜你喜欢
  • 2021-05-19
  • 2021-12-26
  • 2021-10-26
  • 2022-01-08
  • 2022-02-10
相关资源
相似解决方案