Assertions in Java

Assertions in Java - Assertions in Java (JDK 1.4) Jarret...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Assertions in Java (JDK 1.4) Jarret Raim updated by Glenn Blank
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What is an assertion? An assertion is a statement in Java that enables you to test your assumptions about your program. Each assertion contains a boolean expression that you believe will be true when the assertion executes. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.
Background image of page 2
Why use assertions? Programming by contract Pre-conditions Assert precondition as requirement of client Post-conditions Assert post-condition as effect of client method
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Simple Assertion Form The assertion statement has two forms The first is: assert Expression1 ; Where Expression1 is a boolean expression When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no details
Background image of page 4
Complex Assertion Form The second form of the assertion statement is: assert Expression1 : Expression2 ; where: Expression1 is a boolean expression Expression2 is an expression that has a value It cannot invoke of a method that is declared void
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Complex Assertion Form, cont. Use the second version of the assert statement to
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

Assertions in Java - Assertions in Java (JDK 1.4) Jarret...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online