How to use assertions in Java

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.

download

Download the source code for examples in this tutorial. Created by Jeff Friesen for JavaWorld.

What are Java assertions?

Before JDK 1.4, developers often used comments to document assumptions about program correctness. Comments are useless as a mechanism for testing and debugging assumptions, however. The compiler ignores comments, so there is no way to use them for bug detection. Developers also frequently do not update comments when changing code.  

To read this article in full, please click here

\"IT電腦補習
立刻註冊及報名電腦補習課程吧!

Find A Teacher Form:
https://docs.google.com/forms/d/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ/viewform?edit_requested=true#responses

Email:
public1989two@gmail.com






www.itsec.hk
www.itsec.vip
www.itseceu.uk

Be the first to comment

Leave a Reply

Your email address will not be published.


*