Assertion Concept Simplified In Java

0
27
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Assertion in Java

Assertion in java:

In our day to day coding we frequently use System.out.println() statement to debug our code. This is very popular and simple way to debug our development code or test code.One problem of this approach, we can not keep these System.out.println() statement as it is. we need to remove this statement before releasing this code. It is basically overhead for any coder. To overcome this problem Java has come up with a concept called Assertion in java 1.4. prior to java 1.4, in Java 1.3 version it was declared as Identifier.

Design Contract and Assertion:

Design by contract was introduced in Eiffel language. This technique specifies how various components of an application interacts with each others.For this to happen, it defines a contract based on which the components of the application communicate with each other.The DBC technique uses assertions to check whether the application meets the requirements specified in defined contract.
The assertions can be used to test the assumptions made by a programmer in Java.

The DBC technique allows a programmer to provide a detailed specification to create a software according to the user requirements.Based on this specification,the programmer develops the application. The DBC technique uses 3 basic types of assertions to check if the application complies with the specification. They are as follows:

  1. Precondition- An application must satisfy this specific condition before calling an internal component.
  2. Post Condition-An application must satisfy this specified condition after the execution of the external component.
  3. Invariant-An application must always satisfy this specified condition.

 

Real time example of Precondition,Post Condition and Invariant:

let us take an example to extract an element from an Array.

The precondition-

Check the Array to see it it is blank or it has some elements. This condition is checked before extracting an element from the Array.

The post condition-

When we push an element to the Array,we need to check if the element is pushed to the Array correctly.

Invariant-

The number of elements in the Array is greater than equal to zero and should not exceed the capacity of the Array.

Implementing the Assertion:

As assertion is a statement , which contains a Boolean expression that the programmer assumes to be true.If the result of the Boolean expression is true the program execution continues.Here , the assertion ensures that the assumptions made by the programmer are correct and free from errors.
If the result of the Boolean expression is false, the AssertionError exception will be thrown.This exception contains the error information such as file name and the line number in which the error has occurred in the code.

The concepts are as follows..

  • The main advantage of assertion is like System.out.println() statement, we don’t need to delete these statements  as they do not get executed by default.
  • Secondly after release if any bug has come to that particular module we need to fix that. Post bug fix or during debugging again we don’t need to enter System.out.println   or Assert statement. It is already available inside the code hence only enabling that will reduce our work.
  • So it exists in development and test instance but not in production environment
  • It is a better alternative for System.out.println statement.

Let us see more details:

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

Compiler will throw errors saying:

javac E:myProgramassertTest.java

E:myProgramassertTest.java:5: error: as of release 1.4, ‘assert’ is a keyword,and may not be used as an identifier
int assert=13;
^
(use -source 1.3 or lower to use ‘assert’ as an identifier)
E:myProgramassertTest.java:6: error: as of release 1.4, ‘assert’ is a keyword,and may not be used as an identifier
System.out.println(assert);
^
(use -source 1.3 or lower to use ‘assert’ as an identifier)
2 errors

But if we compile against java 1.3, it will compile fine but with warning.

javac -source 1.3 E:myProgramassertTest.java
warning: [options] bootstrap class path not set in conjunction with -source 1.3
E:myProgramassertTest.java:5: warning: as of release 1.4, ‘assert’ is a keywor
d, and may not be used as an identifier
int assert=13;
^
(use -source 1.4 or higher to use ‘assert’ as a keyword)
E:myProgramassertTest.java:6: warning: as of release 1.4, ‘assert’ is a keywor
d, and may not be used as an identifier
System.out.println(assert);
^
(use -source 1.4 or higher to use ‘assert’ as a keyword)
3 warnings

let us see one more example..

class assertTest
{
public static void main(String[] args)
{
assert(false);
System.out.println(assert);
}
}

For this case if we compile with source -1.3, we will get compiler error. but similarly if we compile against 1.4 or upgraded version of java. We will not get the compilation error.
Conclusion: assert could have been used as an identifier in java 1.2 or java 1.3 and in java 1.4 it was promoted as a keyword.So, now ,java 1.4 onward assert can not be used as an identifier.

Types of Assert:

 

  • Simple like assert Expression1;
  • Augmented like Expression1:Expression2

Simple Assertion:

The syntax for simple assertion is
assert(b)
Where b should be Boolean.
It signifies that up to assert statement if b value calculated as true then rest of the statements (below assert statements will be executed). If the assert value calculated as false then it will throw an AssertionError Exception and terminate the program.
let us check out one sample program

class assertTest{
public static void main(String... args)
{
int a=12;
//do some calculation
//do some calculation
//do some calculation
//do some calculation
//do some calculation
//do some calculation
//do some calculation
//do some calculation
assert(a>12);
//do some calculation
//do some calculation
//do some calculation
//do some calculation
//do some calculation
System.out.println(a); 
}
}

The output for this program will be:

javac assertTest.java
java assertTest
output:12
now ..
java -ea assertTest
output:Assertion Error
Exception in thread “main” java.lang.AssertionError
at assertTest.main(assertTest.java:6)
Let us take one more example:


public void division(){
double c=a/b;//b can not be zero
}
//in the above example b can not be zero statement can be replaced by using the 
//assert statement
public void division(){
assert b!=0;
double c=a/b;
}
 

If the expression in the assrt statement is false,it specifies that the program contains errors and this process is referred to as an Assertion failure.

Why -ea is required?

By default the assertions are disabled , but by using “-ea” we are enabling the assertion.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here