Java Reference
In-Depth Information
In this code, you have made an assertion that the value of the variable unitPrice will be greater than 0.0 after
the second statement is executed. What is the probability that the value of unitPrice will be greater than 0.0 after
the second statement is executed? It is difficult to answer this question by just looking at the code. However, you
assume, for the above code to work correctly, that your assertion “the value of unitPrice is greater than 0.0” must be
true. Otherwise, your code will indicate a serious bug in the getPrice() method. It may be obvious for a customer
that the price for an item will be always greater than zero. However, it is not so obvious to a programmer, because he
has to depend on the correct implementation of the getPrice() method. If the getPrice() method has a bug, the
programmer's assertion will be false. If the programmer's assertion is false, he needs to know about the failure of his
assertion, and he needs to take action to fix the bug. If his assertion was false, he would not want to proceed with
the price computations. He would want to halt the price computation as soon as his assertion fails. You have used
a comment to state your assertion. A comment is not executable code. Even if the value of unitPrice is not greater
than zero, your comment is not going to report this error condition or halt the program. You need to use the assertion
facility in such cases to receive a detailed error message and halt the program.
You can make an assertion in Java using an assert statement. The syntax for an assert statement comes in
two forms:
// Form #1
assert booleanAssertionExpression;
// Form #2
assert booleanAssertionExpression : errorMessageExpression;
An assert statement starts with the assert keyword, which is followed by a boolean assertion expression that is
the condition that a programmer believes to be true. If the assertion expression evaluates to true , no action is taken.
If the assertion expression evaluates to false , the runtime throws a java.lang.AsssertionError .
The second form of the assert statement syntax allows you to specify a custom error message expression
when the assertion error is thrown. The assertion condition and the custom message are separated by a colon. The
errorMessageExpression does not have to be a string. It could an expression that may evaluate to any data type,
except the void data type. The runtime will convert the result of the error message expression to string. You can
rewrite the code shown previously to take advantage of the assert statement, like so:
int x = 10 + 15;
assert x == 25; // Uses the first form of the assert statement
int z = x + 12;
Here you replaced the comment with an assert statement. All you need to specify is the condition you assert
(or believe) to be true. You used the first form of the assert statement. You did not use any custom message when
your assertion fails. When the assertion fails, the Java runtime provides you with all details such as line number,
source code, file name, etc. about the error.
In most cases, the first form of the assert statement is sufficient. If you think some values from the program at
the time of error may help you diagnose the problem better, you should use the second form of the assert statement.
Suppose you want to print the value of x when the assertion fails. You could use the following snippet of code:
int x = 10 + 15;
assert x == 25: "x = " + x; // Uses the second form of the assert statement
int z = x + 12;
If you want just the value of x and nothing else, you can use the following snippet of code:
int x = 10 + 15;
assert x == 25: x; // Uses the second form of the assert statement
int z = x + 12;
 
Search WWH ::




Custom Search