Summary on the java assert

Do you want to say goodbye system.out.println () test of the times, then re-assert the beginning, may you begin to feel too much trouble, but you get used to you handy. If you Nengkanmingbai boolean? A: b the ternary operator, then congratulations you assert that you are basically understood, do not believe? Then look down

Assertion mechanism is to support jdk1.4 or later. He has a switch function (will be introduced below), you would like to open to open, you would like to off on off. The output of this statement than a screen full of professional more.

Assert expression

assert expression;

assert expression1: expression2;

In the two expression, expression1 represents a boolean expression, expression2 represents a basic type or an object (Object), basic types, including boolean, char, double, float, int and long. Since all sub-classes for the Object class, so this parameter can be used for all objects.


assert value> 5;

assert accontBalance> 0;

assert isStatusEnabled ();

assert a> 5: "TEST ASSERT"

What is absolutely the switch, nothing more than cases of passing a parameter: java-ea your JAVA program.

  1. Parameter-esa and-dsa:
    Their meaning is open (closed) system class assertion functions. Since the new version of the Java system class, also made the assertion statement, so if users need to observe their operation, they need to open the system class assertion capabilities, we can use open-esa parameters, use the-dsa parameter close. -Esa and-dsa's full name-enablesystemassertions and-disenablesystemassertions, full name and short name are the same function.
  2. Parameter-ea and-ea:
    Their meaning is open (off) the user class assertion Function: With this parameter, the user can open the assertion of some class or package function, the same users can also turn off some of the classes and packages of assertion functions. Open the assertion function parameters-ea; if you do not take any parameters, that opens all user classes; if the name or class name with package that opens the class or package; if the package name followed by three points, on behalf of the bag and its sub-package; if only three points, on behalf of unnamed package. Close assertion feature parameter-da, using methods similar with-ea.
    -Ea and-da's full name-enableassertions and-disenableassertions, full name and short name are the same function.
    The following table that the parameters and their meaning, and there are examples of how to use.
    Parameter Example Explain
    -Ea java-ea Open to all users of the assertion class
    -Da java-da Close all user classes assertion
    -Ea: <classname> java-ea: MyClass1 Open MyClass1's assertion
    -Da: <classname> java-da: MyClass1 Close MyClass1's assertion
    -Ea: <packagename> java-ea: pkg1 Open pkg1 assertion package
    -Da: <packagename> java-da: pkg1 Close pkg1 assertion package
    -Ea: ... java-ea: ... Open the default package (unknown package) the assertion
    -Da: ... java-da: ... Close the default package (unknown package) the assertion
    -Ea: <packagename> ... java-ea: pkg1 ... Open pkg1 package and its sub packages of assertion
    -Da: <packagename> ... java-da: pkg1 ... Close pkg1 package and its sub packages of assertion
    -Esa java-esa Open the System class assertion
    -Dsa java-dsa Shut down the system class assertion
    Combined use of java-dsa: MyClass1: pkg1 Close MyClass1 and pkg1 assertion package

    ... On behalf of them, this package and its sub-packages meaning. For example, we have two packages for the pkg1 and pkg1.subpkg. So pkg1 ... on behalf of pkg1 and pkg1.subpkg two packages.
    In addition, Java programs can also be dynamic in order to open and close some of the classes and packages of assertion capabilities, Java Class and ClassLoader repair the realization of the increase of several operations assert for the API. The following briefly the role of several API.
    ClassLoader class, several related API:
    setDefaultAssertionStatus: for open / close assertion feature setPackageAssertionStatus: for on / off some of the packages assertion functions setClassAssertionStatus: for on / off some kind of assertion functions clearAssertionStatus: assertion functions to close

Talk so much to see an example of the whole:

public static void main (String [] args) (

int a = 5;
assert a == 6: "error";
System.out.println (a);


The world's most simple test, you can modify the value to see a different effect; is simple.

Note: The first assertion is not supported inheritance, if drastic if the superclass is not open, open the sub-category, then only the implementation of the subclass.

Categorically that can modify the variable values, what it means for everyone to start a test program outright. Make use of the assertion changes the characteristics of variable

/ / Public static void main (String [] args) (
/ / Boolean assertFlag = false;
/ / Assert assertFlag = true;
/ / If (assertFlag == false) (
/ / System.out.println ("assert have not opened");
/ /) Else (
/ / System.out.println ("assert is openning");
/ /)
/ /
/ /)

The summary data from the network
标签: quot, parameters, object object, java program, java system, assertion, capabilities, boolean expression, ternary operator, support jdk1, program parameter
分类: Java
时间: 2010-04-14


  1. Java assert - assert

    Java assert - assert Starting from the 1.4 version of Java provide a new feature - assert (assertion). In soft ...
  2. Summary of basic java questions, one of Noodles

    1. Abstract: the abstract is a topic neglected in the current goal has nothing to do with those aspects in ord ...
  3. java assert [转]

    一.assertion的意义和用法 J2SE 1.4在语言上提供了一个新特性,就是assertion功能,它是该版本在Java语言方面最大的革新. 从理论上来说,通过 assertion方式可以证明程序的正确性,但是这是 ...
  4. java assert

    断言在默认情况下是关闭的,要在编译时启用断言,需要使用source1.4标记 既javac source1.4 ,在运行时启用断言需要使用 -ea参数 .要在系统类中启用和禁用断言可以使用 -esa和 ...
  5. Summary on the java webservice technology

    As the use of the new project to webservice, finally spent two days of the look, huh, huh, Thank javaeye, abov ...
  6. Summary commonly used java Utils

    HibernateUtils public class HibernateUtil ( private static final SessionFactory sessionFactory; static ( try ( ...
  7. Java inner class summary

    Java inner class summary In fact, Java inner class J2EE programming using less, but in the window is especiall ...
  8. java assertion assert

    Initial use java assert assertion: assertion open to assert that use of the main sum up how to use assertions ...
  9. Java assertions (assert) - to switch

    I. Introduction In C and C + + language has the key to assert that claim. In Java, there are also assert keywo ...
  10. Java trap the assert keyword

    Java trap the assert keyword I. Introduction In C and C + + language has the key to assert that claim. In Java ...
  11. java interview questions large aggregate sum

    java interview questions Pen ~ a very comprehensive summary of major java interview questions large aggregate ...
  12. java mail system

    Morning classes start: ×××××××××××××××××××××××××××××××××××××××××××××××××× ×××××××××× We require knowledge and ...
  13. Java and SAP's three kinds of data communication

    JAVA and SAP data interact Summary RFC ways: Java programs to directly access SAP through RFC object (or funct ...
  14. Affairs and java services (Abstract)

    First, what is JAVA Services The usual notion that the only database-related matters. Transaction is subject t ...
  15. Maximize Java performance on AIX, Part 1: Basic

    Introduction There are several can be used for the IBM eServer pSeries running AIX platform performance optimi ...
  16. Thinking in java Chinese chm format to share with everyone

    Introduction 1. Premise 2. Java Learning 3. Goal 4. Online Document 5. Chapter 6. Practice 7. Multimedia CD-RO ...
  17. Recognizing class

    Java Encryption: message digest. A message digest is the digital fingerprint of a data block. Namely, an arbit ...
  18. JAVA IO design pattern thorough analysis of the (transfer)

    1. Introduction (overview of Java's IO) No matter what kind of programming language, input with the output are ...
  19. JAVA encryption algorithm on the use case realization

    JAVA encryption algorithm on the use case realization MD5/SHA1, DSA, DESede / DES, Diffie-Hellman used Typical ...