为什么在Java中默认情况下未启用断言? [英] Why assertion is not enabled by default in java?

查看:100
本文介绍了为什么在Java中默认情况下未启用断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下未启用断言,因此我们必须通过将 -ea 作为 jvm参数 so,

By default assertion is not enabled so we have to enable it by passing -ea as jvm argumentso,

  1. 为什么默认情况下未启用它?
  2. 它的确切用途是什么?
  3. 如果启用,是否会导致性能或其他任何问题?

推荐答案

对于1,2),每个断言都包含一个布尔表达式,您认为在执行断言时该表达式将为true.如果不正确,系统将引发错误.通过验证布尔表达式确实为真,断言可以确认您对程序行为的假设,从而增强了您对程序没有错误的信心.

For 1,2)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.

对于3)当您告诉编译器断言的代码返回了一些您已经期望的结果时,如果代码未返回与您期望的结果相同的输出,则可能会出现致命错误或意外的内存泄漏除了断言错误.

For 3) As you tell to compiler that the asserted code returns some thing that you expected already the outcome.If the code doesn't return the same output as you expected then there is chance of having fatal errors or unexpected memory leaks other than Assertion errors.

希望这会有所帮助!

文档 http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

这篇关于为什么在Java中默认情况下未启用断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆