Assertion (assert)

Assertions are programming terms, expressed as the number of Boolean expressions, programmers believe the expression is true at a particular point in the program, you can enable and disable assertions verified at any time, so you can enable assertions at the time of testing and deployment when disable assertions.
Simply put, refers to the assertion, the judge explained the results, such as watermelons selling watermelons old man, pointing to his family to say this is a watermelon, then the assertion will be established. But the old man selling watermelons, pointing to his family to say I sell watermelon watermelon is sweet NO.1 of the world. So we know that there is great uncertainty, so this assertion will not be established . Java uses assert keyword is assert. IDEA, JavaSE engineering need to turn assertions   reference address: https: //www.cnblogs.com/noKing/p/7978092.html way:







    private static void test1() {
        String str = null;
        /*
            assert str != null;
            等同于:
            if (str == null) throw new AssertionError();
         */
        assert str != null;
        System.out.println("str = " + str);
    }

running result:

 

 

 Second way:

    Private  static  void test2 () { 
        String STR = null ;
         / * 
            equivalent to: 
            IF (STR == null) AssertionError with the throw new new ( "I:" + STR); 
         * / 
        Assert STR =! null : "I:" + STR; 
        System.out.println ( "STR =" + STR); 
    }

running result:

 

 

Three ways:

    private static void test3() {
        int i = -1;
        /*
            等同于:
            if ((i <= 0)) throw new AssertionError();
         */
        assert (i > 0);
        System.out.println("i = " + i);
    }

running result:

 

 

summary:

  In fact, the assertion is an abnormal approach, a little more assertive, like I assert: From my side had to charge 20 yuan to the past. To 19 of them. It is generally used to determine whether the object variable is null. And the object is null usually throw an exception.





Guess you like

Origin www.cnblogs.com/yuanke-blog/p/11955956.html