本文介绍了Java断言错误不会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的assert语句没有产生任何结果?我认为第一个assert语句应该失败,但是我看不到Eclipse上显示任何内容.

Why doesn't my assert statement yield any result?I think the first assert statement should fail, but I don't see anything being displayed on Eclipse.

我正在使用Eclipse运行该程序.

I'm using Eclipse to run this program.

package java.first;

public class test {
  public static void main(String[] args) throws Exception {
    String s = "test1";
    assert (s == "test");
    s = "test";
    assert (s == "test");
  }
}

推荐答案

您需要在JVM选项中设置 -ea (启用断言).

You need to set the -ea (Enable Assertions) in your JVM options.

不相关,但是您几乎总是想要 string1.equals(string2),而不是 == .

Unrelated, but you almost always want string1.equals(string2) rather than ==.

这篇关于Java断言错误不会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:56