Assert in Java)

Source: Internet
Author: User

 


We know that C/C ++ has assert ). The assertion feature is also added after Java SE 1.4.

Assertions are used to facilitate program debugging and are not part of a published program. Understanding this is critical.

By default, JVM disables assertions. Therefore, to debug the program using assertions, you must manually enable the assertions function. When running a Java program in command line mode, you can add the parameter-enableassertions or-ea to open the assertions. You can disable assertions through-disableassertions or-da (optional by default ).

Use of assertions:

Assert is defined by the keyword. Generally, assert has two forms.

1. assert <bool expression>; for example, boolean isStudent = false; assert isStudent;

2. assert <bool expression >:< message>; for example, boolean isSafe = false; assert isSafe: "Not Safe at all ";

It is useless. Here are a few simple examples:

 


The first form:

[Java]
01. public class AssertionTest {
02.
03. public static void main (String [] args ){
04.
05. boolean isSafe = false;
06. assert isSafe;
07. System. out. println ("assertion passed! ");
08 .}
09 .}
Public class AssertionTest {

Public static void main (String [] args ){

Boolean isSafe = false;
Assert isSafe;
System. out. println ("assertion passed! ");
}
}

 


If you are running in command line mode, you must specify to enable the assertion function. For example


[Html]
01. java-ea AssertionTest
Java-ea AssertionTest
If it is in IDE, such as Eclipse, you can set it like this: Run as-> Run deployments-> Arguments-> VM arguments: Enter-ea.

 

 

 

Output result:


[Plain]
01. Exception in thread "main" java. lang. AssertionError
02. at AssertionTest. main (AssertionTest. java: 8)
Exception in thread "main" java. lang. AssertionError
At AssertionTest. main (AssertionTest. java: 8)

 


Second form:


[Java]
01. public class AssertionTest {
02.
03. public static void main (String [] args ){
04.
05. boolean isSafe = false;
06. assert isSafe: "Not safe at all ";
07. System. out. println ("assertion passed! ");
08 .}
09 .}
Public class AssertionTest {

Public static void main (String [] args ){

Boolean isSafe = false;
Assert isSafe: "Not safe at all ";
System. out. println ("assertion passed! ");
}
}
Output result:


[Plain]
01. Exception in thread "main" java. lang. AssertionError: Not safe at all
02. at AssertionTest. main (AssertionTest. java: 7)
Exception in thread "main" java. lang. AssertionError: Not safe at all
At AssertionTest. main (AssertionTest. java: 7)

The difference between the second form and the first form is that the latter can specify the error message.

 


Traps:

Assertions are only used to debug programs. Do not write assertions into business logic. For example, consider the following simple example:


[Java]
01. public class AssertionTest {
02.
03. public static void main (String [] args ){
04.
05. assert (args. length> 0 );
06. System. out. println (args [1]);
07 .}
08 .}
Public class AssertionTest {

Public static void main (String [] args ){

Assert (args. length> 0 );
System. out. println (args [1]);
}
}

The assert (args. length> 0) and if (args. length> 0) The meaning is similar, but if the program is released (usually no assertions are enabled), this sentence will be ignored, so the following


[Plain]
01. Exception in thread "main" java. lang. ArrayIndexOutOfBoundsException: 1
02. at AssertionTest. main (AssertionTest. java: 7)
Exception in thread "main" java. lang. ArrayIndexOutOfBoundsException: 1
At AssertionTest. main (AssertionTest. java: 7)
Better alternatives:

JUnit.

 

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.