Writing programs that work correctly at runtime can be challenging. This is because our assumptions about how our code will behave when executed are often wrong. Using Java’s assertions feature is one way to verify that your programming logic is sound.
This tutorial introduces Java assertions. You’ll first learn what assertions are and how to specify and use them in your code. Next, you’ll discover how to use assertions to enforce preconditions and postconditions. Finally, you will compare assertions with exceptions, and find out why you need both in your code.