PDA

Click to See Complete Forum and Search --> : [RESOLVED] use of assertion


newmember
Sep 25th, 2008, 01:05 PM
what is the use of assertion when i can use if statement to get the same result.

could any one please tell me.


thank you.

ComputerJy
Sep 25th, 2008, 08:57 PM
The assert statement evaluates the expression and throws an AssertionError if the value returned is false.. Thats not what if statements are for.

Assertions are mostly used in Testing, besides they can be turned on or off before starting the Application

Further reading (http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html)

newmember
Sep 26th, 2008, 06:03 AM
yeah thanks, now i understood. it mostly used in a development phase. where i would want my application stops if any error occurs. but i can disable it while distributing app.

ComputerJy
Sep 26th, 2008, 08:19 AM
By default, assertions are disabled at runtime. To enable assertions, use the switch -enableassertion (or -ea) as follows:

prompt> java -ea AssertionTest
prompt> java -enableassertion AssertionTest

Where AssertionTest is a class name