O'Reilly logo

Debug It! by Paul Butcher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Assumptions and Assertions

Every piece of code is built upon a platform of myriad assumptions—things that have to be true for it to behave as expected. More often than not, bugs arise because one or more of these assumptions are violated or turn out to be mistaken.

It’s impossible to avoid making such assumptions and pointless to try. But the good news is that not only can we verify that they hold, we can do so automatically with assertions.

What does an assertion look like? In Java, they can take two forms—the first, simpler form is as follows:

 
assert​ condition;

The second form includes a message that is displayed if the assertion fails:

 
assert​ condition : message;

Whichever form you use, whenever it’s executed, an assertion evaluates ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required