i am reading assertions in java.i have material and i had seen some e - material too about assertions.But i am not able to get the main theme why we are using assertions and what it will do.can any one explain to me in general statements?
3 Answers
In simple words
Assertion is mainly used in testing stuff, where you assert [ assert :to declare or affirm solemnly and formally as true; "Before God I swear I am innocent ] something if it fails it will throw AssertionError
Read More
One of the problems with debugging a program is finding out that there is a bug.
Internally you can check the state of all your variables all the time, but that would be time-consuming, but without such checks your internal state might not be what you think it is at any given time, any incorrect state at this point may snowball and crash your system hard later.
Asserts allow you to ensure that the program is in the correct state during development and fail HARD if it's not (with a reasonable message telling you why).
In release code, assert statements do not execute so that you are not slowing the system down (plus, when they are in front of real users, "Fail early, fail loudly" may no longer be the best tactic depending on the application.

- 62,186
- 18
- 105
- 157
-
can you give more explanation about the sentence that "assert statements do not execute so that you are not slowing the system down when they are in front of real users" – satheesh Mar 23 '11 at 06:58
-
@satheesh There is a command line (I believe -ea) to enable assertions. You are supposed to use this command line option during development, but during deployment your customers do not. Without the option, the assertions should be compiled out at load time so they do not exist. – Bill K Mar 23 '11 at 17:04
From similar questions on this server: