Use assertions to make your code more stable
Learn how Java's Assertions is based on how to provide developers with better ways to create stable, high quality, easy-to-debug code. By Josh Street may have not noticed yet, Sun Microsystems has added a new keyword to the Java language. This Assert key is made by JSR41 - a Java Specification Request that ultimately adds real Assertions performance to Java. Because this new Assert keyword and most of the usage familiarity familiar with the C / C developers have a great difference, they will understand that it will benefit you. Java's assertions features are used for a special purpose: it will throw an error when the user-defined Boolean expression is false. More specifically, an assertion is used as a signal that is discovered into false, which is used to indicate that the current function needs to be interrupted. This performance is used very useful in creating a dedicated debug mode in the real product deployment process. Open Assertion You need to open (Enable) before using Assertions, because Java Runtime Environment (JRE) will automatically turn all Assertions to close. You can use the -enableassertions tag (FLAG) (abbreviation is -ea) to turn it; You can use some options with these two tags:
NO arguments refers to this tag applies to all Assertions.
PackageName ... refers to this tag applies to all classes in the packagename package and their subclats.
... refers to this tag applies to unnamed (default) packets in the current directory.
ClassName means that the tag is only available for classname. Look at the following command line:
Java -ea: foo.bar ... -da: foo.bar.old myclass
It is used to turn on the foo.bar package other than foo.bar.old and Assertions in its subdirectories. How to use the Assertion a Java Assertion has two forms. The first form is proven to be desirable:
Assert Some_Boolean_EXPRESSION; this means that if the value of Some_Boolean_Expression is False, an AssertionError will be thrown. Note that this is an error, not an exception; in the same manner, it is treated as a unchecked exception. (To learn more about the difference between errors and abnormalities, please review my article "Error and Exception".) Another more complex form of Assert statement provides some more features:
askSSERT SOME_BOOLEAN_EXPISSION:
Some_non-void_expression
This form is more useful than the previous one. The second expression SOME_NON-VOID_EXPRESSION is incorporated into the constructor for AssertionError. It can display more information when it happens in Assertion. Note that the second parameter may be any object or basic type (including NULL). Listing 1 provides a simple example of the form of the second ASSERT statement. Since AssertionError is a non-check exception, there is no need to capture it (capture it due to the reasons for the demonstration description). In fact, Ignore (don't capture) AssertionerRors may look very well, but you shouldn't do this, because this may make the application in an unstable state, especially for a swings-based program This is even more. There are some cases that cannot use Assertions, for example, try to find the following code: assert isvalid (myObject); even though this looks like a good way, but think about what to do when Assertion is closed - remember this is Its default: this isvalid method will not be executed! In particular, some behavior (methods, operations, etc.) cannot be used for the first parameter of this Assert statement because (unless other specified specified) it will never be executed. You may want to know how to write the code to open Assertions, although this is not a good idea, but it will help the known dependence (Dependency) code is very helpful. To this end, this tip is provided:
Boolean assertsenabled = false;
// here's the trick
Assert assertsenabled = true;
IF (! assertsenable)
Throw new runtimeException
"Asserts Must Be Enabled!");
}
You can use this code to be used in any static initialization class that needs to open Assertions. If the assertion is closed, the Assert statement will not be executed and will throw an exception; otherwise it will continue, the condition is invalid. Solving compatibility issues Java's assertion feature is a language characteristic, not an expansion of API performance. Assertions represents the first significant change in the Java language in multiple versions but also brought back binary compatibility issues. This means that compiled procedures that support Assertions will not be able to run in the old version JRE. In order to make developers more easily create procedures with Assertions, Sun has taken a cautious method. The default status of the program in the JDK 1.4 version is not supported, and the code compiled with Assertions produces the following response:
Warning: as of release 1.4, askERT IS A
Keyword, And May Not Be Used as an identifier
In addition to this Warning, use Assert keywords can also cause many compilation errors. To implement the compiler with Assertions, you need to use a-Source 1.4 tag. For example, the compile command code displayed in the list 1:
Javac -Source 1.4 Assert.java Java New Assertion feature provides new methods for developers using advanced technology (such as Design By Contract) to create a stable, high quality code provides new ways. You need to consider the questions I mentioned, but I think you will also agree that this new feature is quite popular in the Java language - list 1 ---.
Use Assert to test a list 1. This simple example is used to demonstrate how to use Assertions to detect those results that are predictable but unsatisfactory. Public Class Assert
{
Public static void main (string [] args)
{
Try
{
DOSMETHING ();
}
Catch (Assertionerror Error)
{
Error.PrintStackTrace ();
}
}
Public static int dosomething ()
{
INT i = 2;
Assert I! = 2: "I Was 2 for Some Reason";
Return I;
}
}
About the author: Josh Street program is Bank of America's architects. You can contact him through rjstreet@computer.org.