AssertionsAgenda
Introduction:
Assert as keyword and identifier:assert keyword is introduced in 1.4 version hence from 1.4 version onwards we can't use assert as identifier but until 1.3 we can use assert as an identifier. Example: class Test { public static void main(String[] args) { int assert=10; System.out.println(assert); } }Output:
Note: It is always possible to compile a java program according to a particular version by using -source option. Types of assert statements:There are 2 types of asset statements.
Simple version:Syntax: assert(b);//b should be boolean type.
Example: class Test { public static void main(String[] args) { int x=10; ;;;;;;;;; assert(x>10); ;;;;;;;;; System.out.println(x); } } Output: javac Test.java java Test 10 java -ea Test(invalid) R.E: AssertionErrorNote: By default assertions are disable and hence they won't be executed by default we have to enable assertions explicitly by using -ea option. Argumented version:By using argumented version we can argument some extra information with the assertion error.Syntax: assert(b):e; 'b' should be boolean type. 'e' can be any type. Example: class Test { public static void main(String[] args) { int x=10; ;;;;;;;;; assert(x>10):"here x value should be >10 but it is not"; ;;;;;;;;; System.out.println(x); } } Output: javac Test.java java Test 10 java -ea Test(invalid) R.E: AssertionError: here x value should be >10 but it is notConclusion 1: assert(b):e; 'e' will be evaluated if and only if 'b' is false that is if 'b' is true then 'e' won't be evaluated. Example: class Test { public static void main(String[] args) { int x=10; ;;;;;;;;; assert(x==10):++x; ;;;;;;;;; System.out.println(x); } } Output: javac Test.java java Test 10 java -ea Test 10Conclusion 2: assert(b):e; For the 2nd argument we can take method call also but void type method call not allowed. Example: class Test { public static void main(String[] args) { int x=10; ;;;;;;;;; assert(x>10):methodOne(); ;;;;;;;;; System.out.println(x); } public static int methodOne() { return 999; } } Output: javac Test.java java Test 10 java -ea Test R.E: AssertionError: 999If methodOne() method return type is void then we will get compile time error saying void type not allowed here. Various runtime flags:
Example: java -ea -esa -dsa -ea -dsa -esa Test ![]() At the end in both system and non system class assertions are enabled. Example:![]()
It is possible to enable (or) disable assertions either class wise (or) package wise also. Appropriate and inappropriate use of assertions:
![]()
Example: switch(x) { case 1: System.out.println("Jan"); break; case 2: System.out.println("Feb"); break; case 3: System.out.println("Mar"); break; case 12: System.out.println("Dec"); break; default:assert(false); }
AssertionError:
Example: class Test { public static void main(String[] args){ int x=10; try { assert(x>10); } catch (AssertionError e) { System.out.println("not a good programming practice to catch AssertionError"); } System.out.println(x); } } Output: javac Test.java java Test 10 Not a good programming practice to catch AssertionError 10 Example 1: class One { public static void main(String[] args) { int assert=0; } } class Two { public static void main(String[] args) { assert(false); } } Output: Javac -source 1.3 one.java//compiles with warnings. Javac -source 1.4 one.java//compile time error. Javac -source 1.3 Two.java//compile time error. Javac -source 1.4 Two.java//compiles without warnings. Example 2: class Test { public static void main(String[] args) { assert(args.length==1); } } Which two will produce AssertionError? 1) Java Test 2) Java -ea Test//R.E: AssertionError 3) Java Test file1 4) Java -ea Test file1 5) java -ea Test file1 file2//R.E: AssertionError 6) java -ea:Test Test file1To enable the assertions in a particular class. Example 3: class Test { public static void main(String[] args) { boolean assertOn=true; assert(assertOn):assertOn=true; if(assertOn) { System.out.println("assert is on"); } } } Output: Java Test Assert is on Java -ea Test Assert is onIn the above example boolean assertOn=false then answer following questions. Javac Test.java Java Test java -ea Test R.E: AssertionError: trueExample 4: ![]() Example 5: ![]() Example 6: ![]() Note: Because assert statement changes the value of Z. By using assert statement we can not changes the value that is why it is inappropriate. |