Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is java's assert?

Shulou Source: shulou.com Published: 2022-06-01 18:50:42 09月22日 Update

This article mainly explains "what is the assert of java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the assert of java".

01. What is assert?

Assertions are intended to facilitate debugging of the program and are not part of the released program. It is critical to understand this.

The assert keyword indicates an assertion in both C and C++ languages.

Java is no exception, and assertions have been added since Java SE 1.4.

By default, JVM turns off assertions. So if you want to debug the program with assertions, you need to turn on the assertion feature manually.

You can add the parameter-enableassertions or-ea to open assertions when running the Java program in command-line mode.

Assertions can also be turned off with-disableassertions or-da (optional by default).

02. Assertion use

Assertions are defined by the keyword assert, which comes in two forms.

2.1 assert

If the boolean expression is true, the program continues execution. If false, the program throws an AssertionError and terminates execution.

For example:

Public class AssertTest {public static void main (String [] args) {boolean isOk = false; assert isOk; System.out.println ("assertion passed!");}}

Run directly, it is passed directly, because JVM is turned off assertion!

However, we can run it in command mode, with the parameter-ea!

Java-ea AssertTest

For example, Eclipse, you can set it as follows: Run as-> Run Configurations-> Arguments-> VM arguments: type-ea.

Running result:

2.2 、 assert:

If the boolean expression is true, the program continues execution. If false, the program throws a java.lang.AssertionError and enters an error message expression.

For example:

Public class AssertTest2 {public static void main (String [] args) {boolean isOk = false; assert isOk: "No!" ; System.out.println ("assertion passed!");}}

Again, we can run it in command mode with the argument-ea!

Configure the parameters in eclipse, and run the result:

03. Trap

Some students may think that `assert` is similar to `if` judgment, so it can be used in the code!

For example, consider the following simple example:

Public class AssertTest2 {public static void main (String [] args) {int [] is = {1}; assert (is.length > 0); System.out.println (is [1]);}}

The meaning of this sentence assert (is.length > 0) is similar to that of if (is.length > 0). Generally, jvm does not open assertions online. If this sentence is ignored when releasing the program, it will lead to the following error, and the array is out of bounds:

Thank you for your reading, the above is the content of "what is the assert of java". After the study of this article, I believe you have a deeper understanding of what the assert of java is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Programs runs parameters keys commands situations patterns expressions learning keywords content can be passed results errors proximity dispensable code examples information functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Shulou Tech Info OPPO Reno Docker