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.
Printable View
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.
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
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.
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