Assertions in Java

Source: Internet
Author: User
Tags assert

Assertion: The so-called assertion, is a new feature added after jdk1.4.

It is used primarily during code development and testing, to judge certain critical data, and if the key data is not the data your program expects, the program warns or exits.

When the software is officially released, you can cancel the assertion section of the code. Using assert as a keyword in Java, it can be seen that Java is very important to assertions, because if it is not very significant, the development of a class directly, there is no need to define a new keyword.

Grammar 1:assert expression; Expression represents a Boolean type that, if true, continues to run normally, and if False, the program exits

Grammar 2:assert Expression1:expression2; Expression1 is a Boolean expression, Expression2 is a basic type or an object type, and if expression1 is true, the program ignores expression2 to continue running, and if expression1 is false, Run expression2, and then quit the program.

application Example 1:

[Java]View Plaincopy
    1. public class Assertiondemo {
    2. The value of this member variable can change, but must eventually return to the original value 5
    3. static int i = 5;
    4. public static void Main (string[] args) {
    5. Assert i==6;
    6. System.out.println ("If the assertion is normal, I will be printed");
    7. }
    8. }
Operation Result:


Example two:

[Java]View Plaincopy
  1. public class Assertiondemo {
  2. public static void Main (string[] args) {
  3. int i = 3;
  4. switch (i) {
  5. Case 1:
  6. System.out.println ("normal");
  7. Break
  8. Case 2:
  9. System.out.println ("normal");
  10. Break
  11. Case 5:
  12. System.out.println ("normal");
  13. Break
  14. Default
  15. Assert false: The value of "I is invalid"; If the value of I is not what you want, the program warns you to exit
  16. }
  17. System.out.println ("If the assertion is normal, I will be printed");
  18. }
  19. }

Note that in Eclipse, the assertion feature is turned off by default, and if we need to use this feature, we need to open it manually.

To open a step:


In VM arguments input-ea is on (enableassertion) input-da is off (disenableassertion)

Note: The assertion function is used for software development and testing, that is, to delete the assertion of the part of the statement, the structure and operation of your program should not have any changes, do not use the assertion as a function of the program used

 

Assertions in Java

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.