Java notes: Assertions

Source: Internet
Author: User

First, the basic knowledge

Assert can be used in Java to create assertions that assert that a condition should be true during the execution of a program. If the condition is false, a Assertionerror exception is thrown. Assertions are closed by default and are typically not used in the release code, and you do not need to remove assert statements from the published code.

Assertions can be used instead of judgment statements to simplify the inspection of common errors during development.

Second, the switch assertion

IntelliJ idea:

Edit configurations, VM options, fill-ea (enableassertions) or-da (disableassertions)

Iii. Use of assertions

classSolution {Static intval = 1; Static intget () {returnval--; }     Public Static voidMain (string[] args) { for(inti = 0; I < 3; i++)            assertGet () >= 0; //Exception in thread "main" Java.lang.AssertionError//At Solution.main (solution.java:10)    }}
View Code

Java notes: Assertions

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.