Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is java's assert?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report