I disagree with the general statement that assertions are of no use because they can be turned off. I believe they are excellent for that same reason. In the guide to assertions (http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
) nowhere does it recommend replacing exceptions with them - they have new uses.
For example, I have developed an API that reads in a byte array and lazily interprets the data to form various higer level data structures. These structures can be resolved into a byte array which should be identical if no changes have been made to the structures.
I also have an invariant on a data struture that causes the structure to resolve eagerly and regerates the byte array. Running this invariant during normal execution is not feasible - it defeats the purpose of having lazy intialization of the data structures.
My main motivation for doing this with an assertion is that I can disactivate the assertions. Performing this check is very costly - it can slow down usage by orders of magnitude. However, it's extremely handy for finding bugs.
I could have written this as a unit test but I like the fact that it's very light weight and that regular users can activate it easily with a JVM switch. If I give the API to someone, I can just say "turn the -ea option on" and mail me the stack trace. Asking a user to run the unit tests is more laborious and also means I'd have to distribute junit with my production version of the API (or they would have to get it).
The other thing I use assertions for is when I debug. I try to whittle down the stack trace by adding assertions at each point something isn't right. Unlike adding in "System.err"s, I'm quite happy to leave the assertions in because they're not bothering anyone. I actually prefer it if my unit tests fail on an assertion that is in my code rather than an assertion in the unit test. The reason is that the assertion is closer to the problem (thus easier to debug) and travels with my production release (so I get the debugging benfits desribed about).
I agree that assertions are not good everywhere, but they definitely do have their niche.