Introduction to the assertion function in J2SE 1.4

Source: Internet
Author: User
Tags assert constructor
The assertion function provides a mechanism for checking the correctness in the code. This check is usually used in the development and debugging stages and can be closed after the software is deployed. This allows programmers to add debugging check statements in the code, and disable the function after the software is deployed to avoid the impact on the software speed and memory consumption. Basically, the assertion function is a new error check mechanism in JAVA, but this function can be disabled as needed.
In C and C ++, it is usually concluded that functional statements can be preprocessed without being compiled into the final execution code. Because JAVA does not have macro functions, therefore, in the previous java version, it was concluded that the function was not widely used. In JDK1.4, this situation was changed by adding the assert keyword.
The most important feature of this new feature is that the statements can be turned on or off at runtime, which means that these statements with the error check function do not need to be deleted from the source code after the development process ends.
The assertion syntax is very simple, but correct use can help us compile robust (ROBAST) reliable code. In this article, we will not only learn how to write the assertion statement, but also discuss the situation under which the assertion statement should be used.
I. Basic assertion syntax knowledge
We can use the new JAVA keyword assert to write the final statement. A statement has two valid forms:
Assert expression1;
Assert expression1: expression2;
Expression1 is a Boolean expression to be judged. It must be ensured that its value must be true during program execution. expression2 is optional and used when expression1 is false, the constructor passed to the thrown exception AssertionError. Therefore, the type of expression2 must be the parameter type of the valid AssertionError constructor. The following is an example of several statements:
Assert 0 <value;
Assert ref! = Null;
Assert count = (oldCount + 1 );
Assert ref. m1 (parm );
The expression after the assert keyword must be of the boolean type. Otherwise, an error occurs during compilation.
The following is a complete example of using a statement (see the bold statement line ):
Public class aClass {
Public void aMethod (int value ){
Assert value> = 0;
System. out. println ("OK ");
}
Public static void main (String [] args ){
AClass foo = new aClass ();
System. out. print ("aClass. aMethod (1 ):");
Foo. aMethod (1 );
System. out. print ("aClass. aMethod (-1 ):");
Foo. aMethod (-1 );
}
}
This program uses the assert value> = 0; statement to determine whether the parameter in the input aMethod method is not less than 0. If a negative number is input, the AssertionError exception is triggered.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.