Home > Java > javaTutorial > What are Java assertions

What are Java assertions

WBOY
Release: 2023-05-05 10:28:13
forward
1899 people have browsed it

Explanation

1. Assertion is the content introduced after jdk1.4 and is represented by the keyword assert.

2. To check whether the parameters are legal in the program, the if statement is generally used. However, the code still exists in the program after the test is completed. At this time, the assert assertion needs to be introduced. The assertion is not part of the program and will be deleted after the test is completed. This code (Note: idea asserts off by default and needs to be started by adding the -ea running parameter)

Grammar format

assert condition : expression(可省略)
Copy after login

If the condition is not established, the program will execute expression and then terminate Execute and throw AssertionError. If the condition is true, the program will run normally.

Example

public static void main(String[] args){
    int sum = 6;
    assert sum==5 : "sum不等于5";
    System.out.println("---如果断言正常---");
 
}
Copy after login

The above is the detailed content of What are Java assertions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template