Java中的断言语句是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3572528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:58:35  来源:igfitidea点击:

What are assert statements in Java?

javaassert

提问by Vijay Bhaskar Semwal

Possible Duplicate:
What does assert do?

可能的重复:
断言做什么?

Please give me some details with at least one example.

请用至少一个例子给我一些细节。

回答by Sachin Shanbhag

An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

断言是 JavaTM 编程语言中的一个语句,它使您能够测试您对程序的假设。例如,如果您编写一个计算粒子速度的方法,您可能会断言计算出的速度小于光速。

Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.

每个断言都包含一个布尔表达式,您认为该表达式在断言执行时为真。如果不正确,系统将抛出错误。通过验证布尔表达式确实为真,断言确认了您对程序行为的假设,增加了您对程序没有错误的信心。

Check out links below for more details and examples -

查看下面的链接以获取更多详细信息和示例 -

http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html

http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html

http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml

http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml

回答by Noel M

Try this:

尝试这个:

public class AssertionTest {

  public static void main(String args[]) {
     boolean assertTest = true;
     assert assertTest;
     assertTest = false;
     assert assertTest;
  }
}

If you compile and run this, you should have an idea of how the assertion statement works.

如果您编译并运行它,您应该对断言语句的工作原理有所了解。

Update:
As correctly pointed out in the comments, after compilation, you run this as java -ea AssertionTest- the -eaflag enables assertions.

更新:
正如评论中正确指出的那样,编译后,您将其运行为java -ea AssertionTest--ea标志启用断言。

回答by OscarRyz

You use the assertkeyword to verify if something you believe about your code is true.

您可以使用assert关键字来验证您对代码的看法是否属实。

The assertion in nota substitute for code validations, because it can be disabled at runtime ( it is disabled by default ) So, if the assertion is disabled and you use it to control your logic, you'll have undesired results.

在断言进行代码验证的替代品,因为它可以在运行时被禁用(这是默认禁用)所以,如果断言被禁用,你用它来控制你的逻辑,你就会有意外的结果。

For instance:

例如:

class SomeClass {
    public void someMethod( String input ) {
         // do something with the input... 
         String result = processs( input );
         assert result.startWith("OK");
         // continue with your code.. 
         return result;
     }
    ....
 }

In that code, the program does something with the input. Your assumption is, that the result string starts with "OK"always. You put the assertion, to make sure that happens, but you don't put any logic around that ( you don't do anything if that doesn't happens )

在该代码中,程序对输入执行某些操作。您的假设是,结果字符串以"OK"always开头。您提出断言,以确保发生这种情况,但您没有对此提出任何逻辑(如果没有发生,您就不会做任何事情)

When you're testing your code, with assertion enabled, if you notice your result doesn't start with "OK", then the program will stop it's execution.

当您在启用断言的情况下测试代码时,如果您发现结果不是以“OK”开头,则程序将停止执行。

To enable/disble assertions, you have to pass the flag -eato the java

要启用/禁用断言,您必须将标志传递-ea给 java

See : http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.htmlfor more information.

有关更多信息,请参阅:http: //download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html