In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the JDK1.4 feature assert sample analysis, with a certain reference value, interested friends can refer to, I hope you have a lot of gains after reading this article, the following let Xiaobian take you to understand.
Source Code:
/** * Simple examples of the use of the new assertion feature in JDK1.4 * * @author S.Ritter 16/7/2001 **/ 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, it is necessary to add additional parameters during compilation. To compile successfully, you must use javac of JDK1.4 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 arguments (and a numeric command-line argument) to run using assertions, such as:
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 argument is not equal to 10, the assertion function causes the program to throw an assertion error when it runs. Note that an error means that the program has made a serious error and will be forced to exit. Assertions take boolean values and if they are not true throw AssertionError and terminate the program.
Due to programmer problems, 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 a program variable without throwing an error that is difficult to detect without careful inspection. But at the same time we can get a useful feature based on the side effects above, based on which to test whether the assertion is turned on.
/** * Simple examples test enable assertion feature in JDK1.4 * * @author Cherami 25/4/2002 **/ 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 run the above program without the-ea argument, the console will output:
Exception in thread "main" java.lang.RuntimeException: Assertions should be enabled at AssertExample.main(AssertExample.java:14) Thank you for reading this article carefully. I hope that the article "JDK1.4 feature assert sample analysis" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.