Introduction to assert usage in Java

Source: Internet
Author: User

Assert is a new feature introduced in j2se1.4. assertion is a Boolean State included in the Code. Programmers think this state is true. Generally, assert checks program security during development and generally does not use assert during release. Added support for the assert keyword and Java. Lang. asserterror class in 1.4.

First, we need to start with assert from an example.

Public class asserttest
{
Public static void main (string [] ARGs)
{
Asserttest at = new asserttest ();
At. assertme (true );
At. assertme (false );

}
 
Private void assertme (Boolean boo)
{
Assert boo? True: false;
System. Out. println ("true condition ");
}
 
}
If the program contains assert, you must compile it with javac-source 1.4 XXX. java. Otherwise, the compiler reports an error. To make the assert partially run, you must use Java-ea XXX to run it; otherwise, the lines containing the assert will be ignored. Below we run
Javac-source 1.4 asserttest. Java
Java-ea asserttest
The output is as follows:
True Condition
Exception in thread "Main" Java. Lang. assertionerror
At asserttest. assertme (asserttest. Java: 13)
At asserttest. Main (asserttest. Java: 7)
When we run at. assertme (true? True: false is equivalent to assert true; therefore, there is no problem, the program runs down to print the true condition, but runs. assertme (false) is equivalent to assert false. At this time, the interpreter will throw assertionerror and the program will be terminated. You must understand that assertionerror is inherited from error, so you can stop catch it in the program. Of course, you can catch it in the program and then the program can continue to execute. For example:
Public class asserttest
{
Public static void main (string [] ARGs)
{
Asserttest at = new asserttest ();
Try
{
At. assertme (true );
At. assertme (false );
}
Catch (assertionerror AE)
{
System. Out. println ("asserionterror catched ");
}
System. Out. println ("Go on ");

}
 
Private void assertme (Boolean boo)
{
Assert boo? True: false;
System. Out. println ("true condition ");
}
 
}

Assert also has another expression, that is, assert exp1: exp2; exp1 is a Boolean return value expression, and exp2 can be the original data type or object, for example:
Boolean boo = true;
String STR = NULL;
Assert boo = false: Str = "error ";
We have just begun to talk about the form of assert exp1. When the value of exp1 is false, the default constructor of assertionerror will be called, but the form of assert exp1: exp2 is as follows, when exp1 is true, the result of the following expression is calculated and used as the assertionerror constructor parameter. See the following example:
Public class asserttest
{
Public static void main (string [] ARGs)
{
Asserttest at = new asserttest ();
At. assertme (true );
At. assertme (false );

}
 
Private void assertme (Boolean boo)
{
String S = NULL;
Assert boo? True: false: S = "Hello World ";
System. Out. println ("true condition ");
}
 
} This result will be returned during running.
True Condition
Exception in thread "Main" Java. Lang. assertionerror: Hello World
At asserttest. assertme (asserttest. Java: 14)
At asserttest. Main (asserttest. Java: 7)
We recommend that you do not abuse assert because assert is not always enable. In the following two cases, you should not use assert.

  1. Do not check for null or other operations in the public method.
    For example, public int get (string S)
    {
    Assert s! = NULL;
    }
    If you need to check, it is better to throw nullpointerexception through if S = NULL.
  2. Do not use assert to check the return value of a method operation to determine the result of a method operation.
    For example, assert list. removeall (); it seems that there is no problem, but if assert is disable, it will not be executed, so the removeall () operation will not be executed and can be replaced like this.
    Boolean boo = List. removeal ();
    Assert boo;

Let's just say that. Assert is the content of scjp1.4, so it is necessary to understand it.

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.