Java assert keywords

Source: Internet
Author: User

I. Overview

Assert is critical in both C and C ++ languages to indicate assertions.
In Java, the assert keyword also exists, indicating assertions. Both the usage and meaning are similar.

Ii. Syntax

In Java, the assert keyword is introduced from Java SE 1.4. to avoid errors caused by the use of the assert keyword in Java code of earlier versions, by default, Java does not start assertion checks during execution (at this time, all assertion statements will be ignored !), If you want to enable the assertions check, you must enableassertions or-ea.

The syntax of the assert keyword is simple and has two usage methods:

1. Assert <Boolean expression>
If <Boolean expression> is true, the program continues to execute.
If it is false, the program throws an assertionerror and terminates the execution.

2. Assert <Boolean expression >:< error message expression>
If <Boolean expression> is true, the program continues to execute.
If it is false, the program throws a java. Lang. assertionerror and enters the <error message expression>.

3. Application Instances

The following is an example to illustrate its usage:

Public class assertfoo {
Public static void main (string ARGs []) {
// If the result of assertion 1 is true, continue to execute
Assert true;
System. Out. println ("assertion 1 is okay, go! ");

System. out. println ("\ n ----------------- \ n ");

// If assertion 2 is set to false, the program is terminated.
Assert false: "assertion failed. The information of this expression will be output when an exception is thrown! ";
System. out. println ("assertion 2 is okay, Go! ");
}
}

Save the code to C: \ AssertFoo. java and run the Code as follows to view the output result of the console:

1. Compile the program:
C: \> javac AssertFoo. java

2. The program is executed by default. The-ea switch is not enabled:
C: \> java AssertFoo
No problem with assertion 1, Go!

-----------------

Assertion 2 is correct. Go!

3. Enable the-ea switch and execute the program:
C: \> java-ea AssertFoo
No problem with assertion 1, Go!

-----------------

Exception in thread "main" java. lang. AssertionError: assertions failed.
Will be output when an exception is thrown!
At AssertFoo. main (AssertFoo. java: 10)

Iv. traps

The assert keyword is easy to use, but the use of assert often puts you into a deeper trap. Avoid use. After research, the author summarizes the following reasons:

1. The assert keyword must be explicitly enabled at run time to take effect. Otherwise, your assert has no meaning. Currently, mainstream Java IDE tools do not enable the-ea assertion check function by default. This means that if you use IDE tool encoding, debugging and running will be troublesome. In addition, for Java Web applications, the program code is deployed in the container, and you cannot directly control the running of the program. If you must enable the-ea switch, you need to change the running configuration parameters of the Web container. This is inconvenient for program porting and deployment.

2. Replacing if with assert is the second trap. The assert judgment is similar to the if statement, but there is an essential difference between the two: the assert keyword is intended for testing and debugging programs, however, if you accidentally use assert to control the Business Process of the program, removing the assert keyword after testing and debugging means modifying the normal logic of the program.

3. assert failure will lead to exit of the program. This application in a production environment cannot be tolerated. Generally, exception handling is used to solve potential errors in the program. However, it is dangerous to use assertions. Once a failure occurs, the system crashes.

5. Thoughts on assert

Since assert is used to debug the test program and is not used in the formal production environment, we should consider better testing JUint instead, compared with assert, JUint has never been inferior to the key functions provided by assert. Of course, you can use IDE debug for debugging and testing. In this view, the future of assert is dim.

Therefore, you should avoid using the assert keyword in Java, unless on which day Java supports the-ea switch by default, this can be considered at this time. Compare the advantages and troubles that assert can bring to you. This is the principle we choose whether to use.

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.