This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understand "assert"" Watch "Understand "assert"" New topic
Author

Understand "assert"

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

"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
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

How is it used as compared to handling or throwing RuntimeException in the program?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understand "assert"
 
Similar Threads
Question w.r.t Assertions
assertion
Assertion