Java中的assert语句是什么?

发布于 2024-09-15 17:24:04 字数 184 浏览 3 评论 0原文

可能的重复:
断言有什么作用?

请给我一些详细信息,并至少提供一个示例。

Possible Duplicate:
What does assert do?

Please give me some details with at least one example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍风燃霜 2024-09-22 17:24:04

试试这个:

public class AssertionTest {

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

如果您编译并运行它,您应该了解断言语句是如何工作的。

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

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 -ea flag enables assertions.

音盲 2024-09-22 17:24:04

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

中的断言不能替代代码验证,因为它可以在运行时禁用(默认情况下禁用)因此,如果断言被禁用并且您使用它来控制逻辑,您将产生不良结果。

例如:

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;
     }
    ....
 }

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

当你测试你的代码时,启用断言,如果你注意到你的结果不是以“OK”开头,那么程序将停止执行。

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

请参阅:http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html 了解更多信息。

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

The assertion in not a 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 )

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.

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

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

冰魂雪魄 2024-09-22 17:24:04

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

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

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

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

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

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.

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://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文