I recently saw this question and thought that the person who asked question is correct to some degree. The answer informed that we should not use assertions to perform any tasks in our program.
But assertions can act as easy one liners for maintaining loop invariants and program invariants , so that we can check program correctness to a degree.
And why are the assertions necessary even if we have if else?? It just tests a Boolean expression similar things could be done from if -else ladders or so then why bother creating a new keyword Assertion??
A 'task in your program', in context, means something that should be done, and ideally tested for.
Not only is:
assert p != null
shorter and simpler than:
if (p == null) throw new IllegalArgumentException("p is null");
making it an assert clearly documents the fact that it is an internal constraint, not a specified behavior. So you don't need another 4 lines testing it.
Of course, sometimes explicitly specified behavior is what you want, e.g public long-lived APIs.
In other words, while they are similar, it's slightly wrong to use an assert where if/throw is correct, and vice versa.
Nevertheless, a lot of Java code doesn't bother with assert, as that leaves one less decision to make. I'm not sure it would be added to the language if it didn't exist...