Java assert and javaassert

Source: Internet
Author: User
Tags terminates

Java assert and javaassert
Assertions

In Java, similar to C and C ++, The assert keyword is used to indicate assertions. The usage and meaning are similar.

Usage
Assert <boolean expression>

If it is false, the program throws an AssertionError and terminates the execution.

Assert <boolean expression >:< error message expression>

If <boolean expression> is true, the program continues to execute.
If it is false, the program throws a java. lang. AssertionError and enters the <error message expression>.

Code
Public class AssertDemo {public static void main (String args []) {// true, continue to execute assert true; System. out. println ("continue"); System. out. println ("-------- I am a gorgeous split line ------"); // false, the program terminates assert false: "EXCEPTION"; System. out. println ("continue ");}}
I am the dividing line of tiantiao
  • The assert keyword must be explicitly enabled at run time to take effect. Otherwise, your assert has no meaning.
  • Assert cannot replace if. The assert judgment is similar to the if statement, but there is an essential difference between the two: the assert keyword is intended for testing and debugging programs, however, if you accidentally use assert to control the Business Process of the program, removing the assert keyword after testing and debugging means modifying the normal logic of the program.
  • Assert failure will cause the program to exit.

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.