Java know how much (54) assertion detailed

Source: Internet
Author: User
Tags java throws

Assertion assertions are used to justify and test the assumptions of the program, such as "the value here is greater than 5".
Assertions can be completely removed from the code at run time, so there is no effect on how quickly the code runs. Assertions have two ways of using assertions:
    • One is the assert<<-boolean expression >>;
    • The other is the assert<< boolean expression >>:<< detail Description >>.
If the value of the Boolean expression is False, the Assertionerror exception is thrown, and the detail description is a description of the assertionerror exception that is compiled using Javac–source 1.4 Myclass.java in the following example:
1  Public classAssertexample {2      Public Static voidMain (string[] args) {3         intx = 10;4         if(Args.length > 0) {5             Try {6x = Integer.parseint (args[0]);7}Catch(NumberFormatException nfe) {8                 /*Ignore*/9             }Ten         } OneSYSTEM.OUT.PRINTLN ("testing assertion that x = = 10"); A         assertx = = Ten: "Our assertion failed"; -System.out.println ("Test passed"); -     } the}

Due to the introduction of a new keyword, so at compile time need to add additional parameters, to compile successfully, must use JDK1.4 javac and add parameter '-source 1.4 ', for example, you can use the following command to compile the above code:

Javac-source 1.4 Assertexample.java
The above program runs using the Assert feature also requires additional parameters (and requires a number of command-line arguments), such as:
Java-ea Assertexample 1
The output of the program is:
Testing assertion that x = = 10
Exception in thread "main" Java.lang.AssertionError:Our assertion failed
At Assertexample.main (assertexample.java:20)

Because the input parameter is not equal to 10, the assertion feature causes the program to run with an assertion error, which is an error, which means that the program has a critical error and will force the exit. The assertion uses a Boolean value that throws a Assertionerror if its value is not true and terminates the program's run. Assert the recommended use method for validating internal logic in a method, including:
    • Intrinsic invariant type
    • Control process Invariant
    • Post-condition and class invariant
Note: The check for preconditions within the public method is not recommended. The runtime mask asserts that the assertion is to be masked at run time, as in the following ways:
Java–disableassertions or Java–da class name
To allow assertions at run time, you can use the following methods:
Java–enableassertions or Java–ea class name Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

Java knows how much (in) the nesting of Try statements

Java know how much (a) throw: Exception throws

Java know how many () Java throws clauses

Java knows how many (or) finally

Java know how much (52) built-in exceptions

Java know how much (53) Use Java to create your own exception subclasses

Java know how much (54) assertion detailed

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.