Learning about the Java keyword assert

Source: Internet
Author: User

Before learning Java source code, found the assert this less common Keyword. The following is a direct introduction to the use of this Keyword.

What is an assert?

It's the New keyword added after jdk1.4, it's Gone.

What is the role of assert?

The use of assert in many programming languages is assertions.

But what is an assertion?

Is it simple to judge if the Boolean expression is true?

well, with these questions, let's go straight to the Chase.

ASSERT VT Vt. maintain, insist, assert, advocate, claim

By looking at the translation of the assert, we can see that the assert has the meaning of asserting, maintaining, and Persisting.

In other words, the conditions behind the Assert (anti-theft connection: This article starts from Http://www.cnblogs.com/jilodream/) must be met, must be maintained, or there will be errors.

Use of Assert

Use of assert contains two pieces of content

(1) use of keywords in code:

There are two ways to use Assert:

1) assert booleancondition;

Asssert followed by a Boolean Expression.

If the value of the expression is true, the current condition is considered compliant and the business code continues to Execute.

If the value of the expression is false, the Assertionerror error is immediately thrown if the current condition is considered to be noncompliant.

Assertionerror extends Error extends Throwable.throw this class, usually used relatively little, it also has a subclass called Exception. error, like exception, is a serious problem that the system should not attempt to capture.

2) assert Booleancondition:excepiton

Assert is followed by a Boolean expression, followed by an expression that returns a base Type.

When the expression is true, continue shipping (anti-theft connection: This article starts from Http://www.cnblogs.com/jilodream/) line the remaining business code, will not execute ': ' behind the Expression.

When the expression is false, the expression behind ': ' is executed, and the result is placed in the assertionerror exception and Thrown.

Here's a code example:

1  public classAssertstudy2 {3      public Static voidmain (String Args[])4     {5         assert1 = = 1;6System.out.println ("A go!");7System.out.println ("\ n-----------------------------------------------\ n");8         assert1! = 1: "wrong";9System.out.println ("B go!");Ten     } one}

(2) Validity of keywords

Add a breakpoint at the above assert, debug debugging, found that there is no pause at the breakpoint, but directly skipped.

Why is that? This is because the Assert keyword is configured by a Java startup Item.

The switch must be turned on at startup By-ea

Java-ea Assertstudy

This way we will see that the breakpoint on the Assert line is in effect (it is not turned on by default).

and

Java-da assertstudy, so the Assert fails.

Here's how to turn on keyword validation in eclipse:

Select menu: Run--->run ...---> select Arguments tab

In the VM arguments text box, enter:-ea Note there is no space in the middle, if the input-da indicates a forbidden assertion

(if You cannot find the above path, turn on (anti-theft connection: This article starts from Http://www.cnblogs.com/jilodream/) assertion: Java, windows---Preferences Installed jres, Click on the jdk, Edit, Default VM Arguments text box, enter:-ea)

On the necessity of assert

Through the Assert narrative, we find that he is very similar to the IF in Java. So why is Java adding such a keyword? and add it in a later version of jdk1.4?

Note that the description of the assert and the exception thrown is an Error.

That is, the assert is intended to be in the environment, in the normal use of the situation, the problem will not appear in the condition of Judgment. The code often appears in core code such as the base class, the framework class, the tool class, and so On. And in the normal operation of these code, there is no parameter exception (anti-theft connection: This article is starting from Http://www.cnblogs.com/jilodream/) of the Scene. however, once some key values have been changed by reflection, Dynamic proxy, and so on, a large number of abnormal scenes will Occur. And if we are to protect these scenes and add a lot of basic will not be in effect if judgment, then these basic will not work if judgment, not only will seriously affect the readability and brevity of the code, but also make the reader feel that these abnormal scenes will occur frequently, at the same time, the performance of the system has a certain impact.

And assert can effectively pass the configuration item, control this code to take effect, this actually is a very graceful behavior.

PS after writing this paragraph, feel very much TV shopping feeling ....

Some other situations.

1,-ea And-da can effectively point to the class and package path of a certain level, making it more flexible to control the effectiveness of the Assert. The specific use is as Follows:

-ea Java-ea Open Assertion for all user classes

-da Java-da Close the assertion of all user classes

-ea:java-ea:myclass1 Open MyClass1 's assertion

-da:java-da:myclass1 off MyClass1 's assertion

-EA:JAVA-EA:PKG1 Open the PKG1 package Assertion-da:java

-DA:PKG1 closing the PKG1 package assertion

-ea: ... java-ea: ... Open the assertion of the default package (nameless Package)

-da: ... java-da: ... Turn off the assertion of the default package (unnamed Package)

-ea: java-ea:pkg1 ... Open the assertion of the PKG1 package and its child packages

-da: java-da:pkg1 ... Close the assertion of the PKG1 package and its child packages

-esa Java-esa Open the System class assertion

-DSA JAVA-DSA shut down the system class assertion

2, the use of assert, is that you know this thing in normal circumstances is absolutely not happen, but you also know that the OS, the JVM is a matter of occasional inexplicable error, but also to protect someone who calls your code, and you think differently, the wrong call your Code. So:

1) assert is often placed in the User's core processing code, look at the Java source code, you will find that the source has a lot of use of the Assert Keyword.

2) assert deals with situations that do not normally occur under normal circumstances, so use assert in an ordinary business process.

3) assert is not an inherited

If you turn on the assert of the parent class, when you run to the Assert method of the subclass, the subclass is not turned on by Default.

conversely, If you turn on the assert of a subclass and run to the Assert method of the parent class, the assert of the parent class is not turned On.

Reference documents

Http://blog.sina.com.cn/s/blog_95feae0d0101hhcg.html

http://lavasoft.blog.51cto.com/62575/43735

http://www.zhihu.com/question/24461924

Learning about the Java keyword assert

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.