New Assert assertion method in Java

Source: Internet
Author: User
Tags assert getmessage

As the Java language develops, debugging is tiring for the debugger, but the Java development is somewhat convenient for them, and there is now an assertion that the Assert keyword can be used to judge a Boolean attribute. is true or FALSE, but some programming software Eclipse and MyEclipse by default do not turn on this feature,

How to open assertions (assert) in Eclipse by two: 1.Run-> Run configurations-> arguments page check-> VM arguments text box with assert open flag:- Enableassertions or-ea on it, is in the box here directly input can be, of course, you can also enter the-da, this is used to turn off this function, of course, we will always put the side of the open, the other lever of the-ea command is to open the user-defined package , but if you need to open a system-defined package, or you need to enter-esa after entering the-ea line, and then enter-esa, remember, there is no space ... Click Run to confirm when you have finished typing.
2. In MyEclipse, Windows-> Preferences->java->installed JREs Click on the JDK->->edit VM being used Arguments text box enter:-ea (I used this method, this is just the same)
Now we can test if it's successful, and we're going to call this function in the main function public static String Isassertionok () {
Boolean ok=false;
Assert ok=true;
return OK? " Congratulations, Assert function has been opened successfully ":" Sorry, assert function has not been opened successfully, "
+ "Please review the above method, thank you";
}

What the results of the </pre> run. <p></p><pre code_snippet_id= "1973934" snippet_file_name= "blog_20161108_3_5077345" name= "Code" class = "plain" > program error
-------------
program is normal
-------------
it is true!
Null
s=null
check string null is null string     true  s=null
check string null is     null string true
After reading this, I think you should have a lot of feelings.

This function can determine whether a Boolean type is true, and if so, the assert Boolean: "program error"; If the Boolean is true, then the colon is not read. If it is false, then the colon will be read as an error message, that is, will throw an error, the type of the error is assertionerror, so here you need a try...catch statement,

Assert expression1;
Assert Expression1:expression2;
If expression1 is true, no errors are thrown, the program runs correctly, and Expression2 does not execute.
If Expression1 is false, an exception is thrown, the program interrupts and Expression2 executes.

In general, do not use the return value of the function in expression1, expression2;
Do not use it to check input parameters in the public function, but you can use it to detect input parameters in private functions.

When used, compile-time needs to use Javac-source, execution requires the use of Java-ea.


Personal understanding: Use assert only to help us debug the program, so the principle followed by Assert is "not to have any change in the structure of the program because of the presence of an assert", and plainly "if you delete the Assert part,    The program still doesn't have any problems, but it doesn't help us to check out some bugs, so you shouldn't use a function in an expression when you use Assert, because once you delete the Assert statement, the structure of the program changes, which is inconsistent with the principle mentioned above. In this section, we will consider the relationship between assertion and inheritance, and study how the assert is positioned. If the assertion of a subclass is opened, then the assertion of its parent class is executed.

The following example shows if an Assert statement is in the parent class, and when its subclass calls it, the Assert is false. Let's see if the assertion is processed under different circumstances.

Class Base {public
	void Basemethod () {
		try {=
			false: ' assertion failed:this is base ';/Always assertion failure 
  } catch (Throwable e) {
			//Todo:handle exception
			Cout.addline (E.getmessage ());
		System.out.println ("Base method");
	}
	
Class Exercise extends Base {public
	void Derivedmethod () {
		try {
			assert false: ' assertion Failed:this is Derive ";//Always assertion failure
		} catch (Assertionerror e) {
			//Todo:handle exception Cout.addline
			( E.getmessage ());
		}
		System.out.println ("Derived method");
	}
	public static void  F () {
		try {
			assert false: ' d ';
		} catch (Assertionerror e) {
			//Todo:handle Exception
			Cout.addline (E.getmessage ());
		}
	public static void Main (string[] args) {
		Exercise derived=new Exercise ();

			Derived.basemethod ();
			Derived.derivedmethod ();
		
		
	}
The results of the operation are:
Assertion Failed:this are base method
assertion Failed:this are derive
Derived method
It should be understood now ...

If you still have questions, you can add me QQ 2553627958







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.