Java keyword assert learning, javaassert
When learning the java source code, I found the uncommon keyword assert. The following describes how to use this keyword.
What is assert?
It is a newly added keyword after jdk1.4.
What is the role of assert?
The use of assert in many programming languages is assertion.
But what are assertions?
Just to judge whether the Boolean expression is true?
Well, with these questions, let's go straight to the question.
Assert vt. Maintenance, persistence; assertions; claims.
By viewing the translation of assert, we can see that assert has the meaning of proposition, maintenance and persistence.
That is to say, the condition behind assert (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) must meet, must be maintained, otherwise there will be an error.
Use of assert
The use of assert contains two parts
(1) Use of keywords in code:
There are two ways to use assert:
1) assert BooleanCondition;
Asssert is followed by a Boolean expression.
If the expression value is true, the current condition is deemed to meet the requirements and the business code is executed.
If the expression value is false, an AssertionError is thrown immediately if the current condition is deemed to be invalid for a long time.
The AssertionError extends Error extends Throwable. Throw class is rarely used. It also has a subclass called Exception. Similar to Exception, Error is a serious problem that should not be captured by the system.
2) assert BooleanCondition: reset iton
Assert is followed by a Boolean expression and a return value of the basic type.
When the expression is true, continue to run (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) line of the remaining business code, will not execute ':' behind the expression.
When the expression is false, the expression behind ':' is executed, the result is placed in the AssertionError exception, and thrown.
The following is a sample code:
1 public class assertStudy 2 { 3 public static void main(String args[]) 4 { 5 assert 1 == 1; 6 System.out.println("A Go!"); 7 System.out.println("\n-----------------------------------------------\n"); 8 assert 1 != 1 : "Wrong"; 9 System.out.println("B Go!");10 }11 }
(2) keyword Validity
Add a breakpoint at the above assert. During debugging, it is found that there is no pause at the breakpoint, but it is skipped directly.
Why? This is because the assert keyword is configured by the java startup Item.
Enable the switch through-ea during startup
Java-ea assertStudy
In this way, we can see that the assert line breakpoint takes effect (disabled by default ).
While
Java-da assertStudy, so that assert becomes invalid
The methods for enabling keyword validity in eclipse are as follows:
Choose Run> Run...> select the Arguments tab.
In the VM arguments text box, enter:-ea. Note that there is no space in the middle. If-da is input, assertions are not allowed.
(If you can not find the above path, please turn on (anti-theft connection: This article first from the http://www.cnblogs.com/jilodream/) Start asserted: windows-> Preferences-> Java-> Installed JREs-> click JDK in use-> Edit-> Default VM Arguments text box and enter-ea)
Necessity of assert
Through the description of assert, we found that it is very similar to if in java. So why should such a keyword be added to java? And add it in a later version like jdk1.4?
Note that the description of assert and the thrown exception are an Error.
That is to say, assert is intended to be a condition for normal use in the environment without any problems. These codes are often used in core codes such as the base class, framework class, and tool class. In the normal operation of these codes, there is no parameter exception (anti-theft connection: This paper first from the http://www.cnblogs.com/jilodream/) scene. However, if some key values are changed through reflection and dynamic proxy, a large number of exception scenarios may occur. If we add a large number of if judgments that will not take effect in order to protect these scenarios, these basic if judgments will not seriously affect the readability and conciseness of the Code, it also makes readers think that these exception scenarios will happen frequently and affect the system performance.
Assert can effectively control whether the code is effective through configuration items, which is actually a very elegant behavior.
Ps after writing this section, I feel very fond of TV shopping ....
Other situations
1.-ea and-da can effectively point to a level in the class and package path, so that the validity of assert can be controlled more flexibly. The specific usage is as follows:
-Ea java-ea opens assertion for all user classes
-Da java-da: Close assertion of all user classes
-Ea: java-ea: MyClass1 open the assertion of MyClass1
-Da: java-da: MyClass1 disable assertion of MyClass1
-Ea: java-ea: pkg1 open the assertion-da: java of the pkg1 package
-Da: pkg1: Disable the assertion of the pkg1 package.
-Ea:... java-ea:... open the assertion of the default package (untitled package)
-Da:... java-da:... disable the assertion of the default package (untitled package ).
-Ea:... java-ea: pkg1. open the assertion of the pkg1 package and its sub-packages.
-Da:... java-da: pkg1. disable the assertion of the pkg1 package and its sub-packages.
-Esa java-esa: Open the assertion of the system class
-Dsa java-dsa: Disable the assertion of the system class
2. The use of assert means that you know that this event will never happen under normal circumstances, but you also know that events in OS and jvm will occasionally encounter inexplicable errors, at the same time, it is not allowed to call your code. Unlike what you think, your code is called incorrectly. Therefore:
1) assert is often placed in your core processing code. by looking at the java source code, you will find that the source code contains a large number of assert keywords.
2) assert is a situation that will never happen under normal circumstances, so it is used in normal business processes.
3) assert is not inherited.
If the assert function of the parent class is enabled, when the assert method of the Child class is run, the Child class is disabled by default.
If you enable the assert of the subclass and run the assert method of the parent class, the assert of the parent class is not enabled.
References
Http://blog.sina.com.cn/s/blog_95feae0d0101hhcg.html
Http://lavasoft.blog.51cto.com/62575/43735
Http://www.zhihu.com/question/24461924