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

How to use the new JDK1.4 feature assertion

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the new features of JDK1.4 assertion, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

One of the new features introduced in JDK1.4 is assert, which provides strong support for program debugging. The following documents are based on SUNTEC content and related content.

Source code:

Public class AssertExample {

Public static void main (String [] args) {

Int x = 10

If (args.length > 0) {

Try {

X = Integer.parseInt (args [0])

} catch (NumberFormatException nfe) {

/ * Ignore * /

}

}

System.out.println ("Testing assertion that x = = 10")

Assert x = = 10: "Our assertion failed"

System.out.println ("Test passed")

}

}

Due to the introduction of a new keyword, you need to add additional parameters during compilation. To compile successfully, you must use JDK1.4 's javac and add the parameter'- source 1.4'. For example, you can compile the above code with the following command:

Javac-source 1.4 AssertExample.java

The above program also requires additional parameters (and a numeric command line argument) to run using the assertion function, for example:

Java-ea AssertExample 1

The output of the program is:

Testing assertion that x = = 10

Exception in thread "main" java.lang.AssertionError: Our assertion failed

At AssertExample.main (AssertExample.java:20)

Since the input parameter is not equal to 10, the assertion function causes the program to throw an assertion error at run time, note that it is an error, which means that the program has a serious error and will be forced to exit. The assertion uses a Boolean value, and if its value is not true, it throws an AssertionError and terminates the program.

Due to problems with programmers, the use of assertions may have side effects, such as:

Boolean isEnable=false

/ /...

Assert isEnable=true

The side effect of this assertion is that it modifies the value of the program variable and does not throw an error, which is difficult to find without careful inspection. But at the same time, we can get a useful feature based on the above side effects to test whether the assertion is turned on.

Public class AssertExample2 {

Public static void main (String [] args) {

Boolean assertEnable=false

Assert assertEnable=true

If (assertEnable==false)

{

Throw new RuntimeException ("Assertions should be enable")

}

}

}

If we do not run the above program with the-ea argument, the console will output:

Exception in thread "main" java.lang.RuntimeException: Assertions should be enab

Le

At AssertExample.main (AssertExample.java:14)

The above is all the content of the article "how to use the New JDK1.4 feature assertion". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

  • How to configure Oracle and H2 dual data sources using SpringBoot

    This article is about how to use SpringBoot to configure Oracle and H2 dual data sources. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look. Configure POM

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

    12
    Report