Assertions

Assertions

Assertions has introduced in 1.4 version. The main objective of assertions is to perform debugging. The traditional way of debugging is to use System.out.println’s. But the main disadvantage of this approach is compulsory we should remove these S.O.P’s after fixing the problem other wise these will execute at run time. Which may effect performance of the system. It may reduce readability of the code and disturbs logging mechanism.

To resolve all these problems sun people has introduces Assertions concept in 1.4 version.

The main advantage of assertions is we can enable or disable assertions based on our requirement. But by default assertions are disabled.

Assertions concept we have to use in Test environment or in Development environment but not in the production.

Identifier Vs Keyword

Assert keyword has introduced in 1.4 version hence from 1.4 version on words we are not allowed to use assert as identifier.

public class Test {
   public static void main(String[] args) {
      int assert = 10;
      System.out.println("assert");
   }
}

 In 1.4 or 1.5 if we compile javac assert.java then we will get the following C.E as of release 1.4 assert is a keyword and may not used as an identifier.

javac –source 1.3 assert.java
java Test
Types of assert statement

There are 2 types of assert statement

1. Simple assert

Syntax

assert <boolean expression> ;
assert(b);

If b is true then our assumption satisfied and hence rest of the program will be executed normally. If b is false then our assumption fails i.e., some where some thing goes wrong and hence the program will be terminated abnormally by raising AssertionException. Once we got AssertionError we will analyse the code and we can fix the problem.

public class Test {
   public static void main(String[] args) {
      int x = 10;
          :
          :
      assert(x>10)
      System.out.println(x);
   }
}
1. javac Test.java
2. java Test
in this case assert statement won’t be executed because it is disable by default.
3. java –ea Test
Then generates assertion error.
Augmented Version

Syntax:

assert <boolean expression> : <message expression> ;
Assert e1:e2;
β€˜e1’ -> should be boolean type.
β€˜e2’ -> any thing is allowed including method calls also 
public class Test {
   public static void main(String[] args) {
      int x = 10;
        .
        .
      assert(x>10):"here the value of x should be > 10 but it is "+x;
      System.out.println(x);
   }
}
javac Test.java
java –ea Test
Exception in thread "main" java.lang.AssertionException: here the value of x should be > 10 but it is 10 

Note

assert e1:e2

Here β€˜e2’ will be executed iff β€˜e1’ is false.

public class Test {
   public static void main(String[] args) {
      int x = 10;
         ;
         ;
      assert(x==0):++x;
      System.out.println(x);
   }
}
javac Test.java
java –ea Test
Exception in thread "main" java.lang.AssertionException: 11
assert(e1):e2;

for e2 any thing is allowed including method calls also But void return type method calls are not allowed. Violation leads to compile time error.

public class Test {
   public static void main(String[] args) {
      int x = 10;
           ;
           ;
      assert(x>0):m1();
      System.out.println(x);
   }
   public static void m1() {
      return;
   }
}
javac Test.java
java –ea Test
Various Run Time Flags
  1. -ea -> To enable assertions in every non-system class(i.e user defined class)
  2. -enableassertions -> To enable assertions in every non-system class(Exactly similar to -ea)
  3. -da -> To disable assertions in every non-system class.
  4. -isableassertions > To disable assertions in every non-system class.(Exactly similar to -da)
  5. -esa -> To enable assertions in every system class
  6. -enablesystemassertion -> similar to -esa
  7. –dsa -> To disable assertions in every system class.
  8. -disablesystemassertions -> similar to β€˜-dsa’
Proper and Improper Use of assertions
  • It is improper to use assert statement for validating the arguments of a public method.
public void withdraw(double amount) {
   assert(amount >= 100);
}
C.E: No C.E, R.E it is improper use.
  • It is improper to use assertions for validating command line argument also, because these are arguments to public main method.
  • It is proper to use assertions for validating private method argument.
  • It is improper to mix programming language with assert statement.
  • In our code if there is any place where the control is not allowed to reach. It is the best place to use the assert statement.
AssertionError
  • It is the child class of Error and it is unchecked.
  • It is not recommended to catch AssertionError by using catch Block. It is stupid type of activity.
public class Test {
   public static void main(String arg[]) {
      int x = 10;
          ;
          ;
      try {
        assert(x>10);
      } catch (AssertionError e) {
        System.out.println("I am stupid...Because I am catching Assertion");
      }
   }
}

Output

I am stupid...Because I am catching Assertion

Assertions
Scroll to top