2

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 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

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 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.
2

From similar questions on this server:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.