This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have tried to figure out the meaning of assert keyword, but even after reading few trusted resources, I am yet to come up with detailed clarification like why use it when it requires clients to run with -ea explicitly and how it performs in terms of exception handling. I need a sound answer, as I am a Java kid. Please explain it all. Thanks a lot just for reading.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
"Assert" is basically just a development and QA sanity check. It's not intended to be part of the normal running of a program, and correct behavior must not rely on it. I don't use it very often, but when I do, it's just to make sure that things that I think must be true are in fact true, in case I missed something or had a mistake in my logic.
Rajdeep Biswas wrote:How is it used as compared to handling or throwing RuntimeException in the program?
A RuntimeException, such as, say, NullPointerException, is expected in a production release of your app if the relevant problem occurs. If you try to dereference a null pointer, it would be incorrect behavior to NOT throw NPE.
In contrast, the assert mechanism is only for development, testing, and debugging. Turning off assertions in production code is required to still result in correct behavior.