20 Assertions
20 Assertions
nd
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
141 142 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Example:
Assertions class Test
{
Agenda public static void main(String[] args)
{
1. Introduction int assert=10;
2. Assert as keyword and identifier System.out.println(assert);
3. Types of assert statements }
i. Simple version }
ii. Argumented version Output:
4. Various runtime flags
5. Appropriate and Inappropriate use of assertions javac Test.java(invalid)
6. AssertionError As of release 1.4, 'assert' is a keyword, and may not be used as an identifier. (Use
-source 1.3 or lower to use 'assert' as an identifier)
Introduction: javac -source 1.3 Test.java(valid)
The code compiles fine but warnings will be generated.
1. The most common way of debugging is uses of sops. But the main disadvantage java Test
of sops is after fixing the bug compulsory we should delete extra added sops 10
otherwise these sops also will be executed at runtime which impacts performance
of the system and disturbs logging mechanism. Note: It is always possible to compile a java program according to a particular version
2. To overcome these problems sun people introduced assertions concept in 1.4 by using -source option.
versions.
3. The main advantage of assertions over sops is based on our requirement we can Types of assert statements:
enable or disable assertions and by default assertions are disable hence after
fixing the bug it is not required to delete assert statements explicitly. There are 2 types of asset statements.
4. Hence the main objective of assertions is to perform debugging.
5. Usually we can perform debugging either in development environment or Test 1. Simple version
environment but not in production environment hence assertions concept is
applicable for the development and test environments but not for the production.
2. Argumented version
assert keyword is introduced in 1.4 version hence from 1.4 version onwards we can't use Simple version:
assert as identifier but until 1.3 we can use assert as an identifier.
Syntax: assert(b);//b should be boolean type.
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
143 144
JAVA Means DURGA SIR JAVA Means DURGA SIR
If 'b' is true then our assumption is correct and continue rest of the program {
normally. public static void main(String[] args)
If 'b' is false our assumption is fails and hence stop the program execution by {
raising assertion error. int x=10;
;;;;;;;;;
Example: assert(x>10):"here x value should be >10 but it
class Test is not";
{ ;;;;;;;;;
public static void main(String[] args) System.out.println(x);
{ }
int x=10; }
;;;;;;;;; Output:
assert(x>10); javac Test.java
;;;;;;;;; java Test
System.out.println(x); 10
} java -ea Test(invalid)
} R.E: AssertionError: here x value should be >10 but it is not
Output: Conclusion 1:
javac Test.java
java Test assert(b):e;
10
java -ea Test(invalid) 'e' will be evaluated if and only if 'b' is false that is if 'b' is true then 'e' won't be
R.E: AssertionError evaluated.
Note: By default assertions are disable and hence they won't be executed by default we
have to enable assertions explicitly by using -ea option. Example:
class Test
Argumented version: {
public static void main(String[] args)
By using argumented version we can argument some extra information with the {
assertion error. int x=10;
Syntax: assert(b):e; ;;;;;;;;;
'b' should be boolean type. assert(x==10):++x;
'e' can be any type. ;;;;;;;;;
System.out.println(x);
}
}
Output:
javac Test.java
java Test
10
java -ea Test
10
Conclusion 2:
assert(b):e;
Example: For the 2nd argument we can take method call also but void type method call not
allowed.
class Test
nd
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
145 146 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
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: 999
If methodOne() method return type is void then we will get compile time error saying
void type not allowed here. At the end in both system and non system class assertions are enabled.
Example:
Various runtime flags:
Example: We can use these flags in combination also but all these flags will be executed
from left to right.
Example:
java -ea -esa -dsa -ea -dsa -esa Test
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
147 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
148
JAVA Means DURGA SIR JAVA Means DURGA SIR
Example:
It is possible to enable (or) disable assertions either class wise (or) package wise also.
Example:
switch(x)
{
case 1:
System.out.println("Jan");
break;
case 2:
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
149 150
System.out.println("Feb"); try {
break; assert(x>10);
case 3: }
System.out.println("Mar"); catch (AssertionError e) {
break; System.out.println("not a good programming practice to
case 12: catch AssertionError");
System.out.println("Dec"); }
break; System.out.println(x);
default:assert(false); }
} }
Output:
It is always inappropriate to use assertions for validating public method javac Test.java
arguments. java Test
It is always appropriate to use assertions for validating private method 10
arguments. Not a good programming practice to catch AssertionError
It is always inappropriate to use assertions for validating command line 10
arguments because these are arguments to public method main. 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
AssertionError: {
public static void main(String[] args)
{
1. It is the child class of Error and it is unchecked. assert(args.length==1);
2. Raised explicitly whenever assert statement fails. }
3. Even though it is legal but it is not recommended to catch AssertionError. }
Which two will produce AssertionError?
Example:
1) Java Test
class Test {
2) Java -ea Test//R.E: AssertionError
public static void main(String[] args){
3) Java Test file1
int x=10;
4) Java -ea Test file1
nd
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
151 152 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGA SIR JAVA Means DURGA SIR
Example 4:
Example 6:
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
153 154
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.
DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, DURGASOFT, # 202,2ndFloor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
155 156