2.Flow Control, Assertions And Exception Handing

1.1assertions may be used to validate the input parameters of a private method. This is because, private methods are called only by the developer of the class. Therefore, if a private method is called with an invalid parameter, this problem should be rectified at the development stage itself. It cannot occur in the production stage, so there is no need to throw an explicit exception.
If the boolean expression is false, a java.lang.AssertionError is thrown. AssertionError extends from java.lang.Error.
The second operand can be null but it cannot be void. In the above case, if value is not 10, it will throw new AssertionError(null).

1.2 assert <boolean_expression> : <any_expression_but_void>;
1.3 Each enable or disable modifies the one before it. This allows you to enable assertions in general, but disable them in a particular package:
java -ea -da:<package>... myPackage.myProgram


2.break cannot be used outside of a loop or a switch
3.continue cannot be used outside of a loop(continue can be used only inside a 'for', 'while' or 'do while' loop.)

4            int x;
//            while (false) { x=3;System.out.println(x); }    //Unreachable code   
            if (false) { x=3; System.out.println(x); }
           
            for( int i = 0; i< 0; i++) x = 3 ;            
//            for( int i = 0; false; i++) x = 3;              //Unreachable code
            
   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章