Java Assertion assert

Source: Internet
Author: User

public class welcome{public    static void Main (string[] args) {        assert false;        System.out.println ("Welcome World");}    }

CMD under input command

Javac Welcome.java

Java Welcome

Output to

Welcome World

Java does not open assertions by default, in which case the Assert statement will be skipped and not executed.

In order to execute the Assert statement, you need to re-enter the command after adding the-ea parameter, cmd

Javac Welcome.java
Java-ea Welcome

At this point the output is

Exception in thread "main" Java.lang.AssertionError at        welcome.main (welcome.java:3)

The Java assertion mechanism requires that the Boolean expression following the Assert must return a true value before it continues to execute, and if not true, throws a exception, which is an unchecked exception.

The assert can also return a hint message, as follows

public class welcome{

public static void Main (string[] args) {
Assert false: "assert is false"; separated by a colon
System.out.println ("Welcome World");
}

}

CMD under input command

Javac Welcome.java

Java Welcome

Output to

Exception in thread ' main ' java.lang.AssertionError: assert is false //assert hint
At Welcome.main (Welcome.java:3)

Assertions are a relatively simple mechanism for debugging programs.

Java Assertion assert

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.