Saturday, December 26, 2009

Java Assertion

Java Assertion Claim structure in J2SE 1.4. In support of this facility J2SE 1.4 added the keyword assert to the language, and AssertionError class. A typed statement reviewed boolean expression is true that during the execution of the program. The assertion facility can be enabled or disabled at runtime. Consistency Assertion Assertion statements have two forms, as an expression to say, say expression1: expression2; The first form is simple form of assertion, while the second takes a different form of expression. In both forms of expression is a boolean condition that must be evaluated to true maturity. If the condition of "fake" currency and securities are enabled, AssertionError will be thrown at run time. Some examples, the simple assertion form are as follows. assert value> 5; accontBalance request> 0; application isStatusEnabled (); The expression, the term must make boolean value. The third example isStatusEnabled () are required to boolean value. If the condition is true, execution continues normally, otherwise the AssertionError is thrown. Following program uses simple form of assertion / / AssertionDemo.java class AssertionDemo (public static void main (String args []) (System.out.println (withdrawMoney (1000.500)); System.out.println (withdrawMoney (1000.2000)); ) publishes twice withdrawMoney (double balance, double) (affirming balance> = amount; retu balance - amount;)) In this example, the main method calls withdrawMoney method with balance and height as arguments. The method has a withdrawMoney maintain that controls whether the balance is greater than or equal amount withdrawn. In the first call of the method is executed without any exception, but in the second call AssertionError is thrown if the assertion is enabled at runtime. Enable Assertion assertion of default are not enabled, but the compiler complains that when the request is an identification or label. The following command compiles AssertionDemo with assertion enabled. javac-source 1 / 4 AssertionDemo.java The resulting AssertionDemo class file contains the code statement. By default assertion are disabled in the Java Runtime Environment. The argument eanbleassertion or EA is the assertion that while disableassertion or disables assertions calendar. The following command with the statement AssertionDemo enabled. Java or Java AssertionDemo a enableassertion AssertionDemo second form of assertion, the second for the assertion that takes another expression as an argument. The syntax is to assert expression1: expression2; expression1, which is the condition and to examine the "real" life. This statement is equivalent to assert expression1: throw new AssertionError (expression2); Note: AssertionError exception is disabled, because it is a mistake, that the class. Here, expression2 checks up to a certain value. By default AssertionError does not provide useful message so this form may be useful to review some informative message for the user.

No comments:

Post a Comment