JDK1.4 new feature: assertion

Source: Internet
Author: User
JDK1.4 new feature: assertion-general Linux technology-Linux programming and kernel information. The following is a detailed description. One of the new features introduced in JDK1.4 is assert, which provides strong support for program debugging. The following documents are based on SUNTEC content and related content.
Source code:
/**
* Simple examples of the use of the new assertion feature in JDK1.4
*
* @ Author S. Ritter 16/7/2001
**/

Public class AssertExample {
Public static void main (String [] args ){
Int x = 10;

If (args. length> 0 ){
Try {
X = Integer. parseInt (args [0]);
} Catch (NumberFormatException nfe ){
/* Ignore */
}
}

System. out. println ("Testing assertion that x = 10 ");
Assert x = 10: "Our assertion failed ";
System. out. println ("Test passed ");
}
}

Because a New Keyword is introduced, additional parameters need to be added during compilation. To compile successfully, you must use javac of JDK1.4 and add the 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 also needs to use additional parameters (and a numeric command line parameter) to use the asserted function, for example:
Java-ea AssertExample 1

Program output:
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 function throws an assertion error when running the program. Note that it is an error, which means that the program has a serious error and will be forced to exit. AssertionError is thrown and the program is terminated if its value is not true.
Due to programmer problems, the use of assertions may bring side effects, such:
Boolean isEnable = false;
//...
Assert isEnable = true;

The side effect of this assertion is that it modifies the value of the program variable and does not throw an error. Such an error is hard to find without careful check. But at the same time, we can get a useful feature based on the above side effects and test whether to enable the assertions.
/**
* Simple examples test enable assertion feature in JDK1.4
*
* @ Author Cherami 25/4/2002
**/

Public class AssertExample2 {
Public static void main (String [] args ){
Boolean assertEnable = false;
Assert assertEnable = true;

If (assertEnable = false)
{
Throw new RuntimeException ("Assertions shocould be enable ");
}
}
}

If we do not use the-ea parameter to run the above program, the console will output:

Exception in thread "main" java. lang. RuntimeException: Assertions shocould be enab
Le
At AssertExample. main (AssertExample. java: 14)
Related Article

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.