2

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

  1. Why it is not enabled by default?
  2. What is the exact use of it?
  3. If we enable does it cause performance or any other issue?
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • 3
    1. [Why assertion is not enabled by default][1] 2 & 3. [Use of assertion and do they raise error][2] [1]: http://stackoverflow.com/questions/29120928/why-is-assert-not-enabled-by-default-in-java [2]: http://stackoverflow.com/questions/13218496/what-is-the-use-of-assert-in-java – Gaurav Mahawar May 19 '15 at 11:45
  • 2
    1-2) Assertions are meant to be used as testing tool only, it should not be part of standard code logic so JVM on client side shouldn't have to run it that is why it is by default disabled. 3) depends on assertions code which would normally be skipped. – Pshemo May 19 '15 at 11:45
  • 5
    @GauravMahawar Use `[description](link)` to post links in comments. – Pshemo May 19 '15 at 11:48

2 Answers2

3

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.

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.

Hope this helps!

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

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
1

Assertions can be used to verify (and document) program invariant.

It is not designed to handle exceptional conditions so you don't expect it to make a difference in production environment anyway.

It is debatable whether to enable assertions in production environment, so Java may decide arbitrarily that it will not do so by default, or may do so to not confuse those unaware.

I don't expect assertions to harm performance at all in production environment (provided you don't enable them), they can be easily optimized away (or totally ignored) by the JIT compiler.

ntysdd
  • 1,206
  • 2
  • 9
  • 19
  • 3) since assertions are not enabled by default, it is important that the expression (function or simple expression) does not have side effects. For example, the expression should not change the value of a variable since the behaviour of the program will change when assertions are on. One important use case is to test pre/post conditions in a development or QA environment (using -ea) and be able to remove those tests from production without need to modify code. Some would argue you can achieve similar with aspects, but I find it easier to use assert. – roostergx Mar 23 '16 at 21:59