_________________________________________________
An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
Each assertion contains a Boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. (oracle docs)
Use:
assert Expression1 ;
where Expression1 is a Boolean expression. When the system runs the assertion, like
assert(Expression1);
it evaluates Expression1 and if 'Expression1' is false, it will throws an error 'AssertionError' with no detail message.
If you want to print some message if Assertion Error occuers, use the following format.
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 may simply be a string used as detail message).
Example:
int number = 9;
assert ( number != 10) : "Number is not equal to 10";
// it will be throwing AssertionError with the message "Number is not equal to 10".
An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
Each assertion contains a Boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. (oracle docs)
Use:
assert Expression1 ;
where Expression1 is a Boolean expression. When the system runs the assertion, like
assert(Expression1);
it evaluates Expression1 and if 'Expression1' is false, it will throws an error 'AssertionError' with no detail message.
If you want to print some message if Assertion Error occuers, use the following format.
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 may simply be a string used as detail message).
Example:
int number = 9;
assert ( number != 10) : "Number is not equal to 10";
// it will be throwing AssertionError with the message "Number is not equal to 10".
No comments:
Post a Comment