Rob Prime wrote:Turn them on: use the -ea JVM flag.
Turn them off: don't use that flag.
That's the main thing about assertions - they are conditionally enabled. You should never use any logic that relies on the fact that they are turned on or off. That also means that the assertions cannot have any side effects. The following is just wrong:
Rob Prime wrote:Note that you should never ever ever use assertions to validate input to non-private methods, as you don't fully control who is calling those methods. If assertions are the only validation then that validation can be turned off by simply turning off assertions. So instead of
you should write
For private methods or code points inside methods where input validation already took place, assertions are just fine.
Campbell Ritchie wrote:The negative value is supposed to give an error; your assertion is failing. Try changing the line to
Campbell Ritchie wrote:Try changing m.check(2); to m.check(-99); and see what happens.