Thursday, November 3, 2016

Java Assertions with example

Assert is a keyword,many programmers may not familiar with this word.So, I am posting use of assertions,where we use in real time projects and so on.. It has introduced in java 1.4 version. The java is a keyword called assert,it is used during program development to create assertions. The main objective  of assertions is to perform debugging. That means often it is used during testing to verify that some expected condition is actually met. But  these are not usually used for release code. 

The traditional way of debugging is to use System.out.println, But the main disadvantage of this approach is compulsory we should remove these System.out.println statements after fixing the problem otherwise these will execute at run time which may effect the performance of the system. It may reduce readability of the code and  disturbs logging mechanism. To resolve all these problems sun people introduced assertions concept in java 1.4 version.

Advantage:

We can enable or disable assertions based on our requirement,by default assertions are disabled.

Where to use assertions in the Project:

Assertions can be used in the Testing environment or Development environment but not in the production.

Some programmers have in confused about was assert is identifier (or) keyword. From java 1.4 version on wards assert is a keyword but not identifier.

Types of assert statements:

There are two types of assert statements 
1. simple assert
2. Augmented assert

1. Simple assert:

Syntax: assert condition;

In the above syntax the condition is an expression that must evaluate to a Boolean result. 

Example:

 assert<Boolean expression>;
assert(b);

If b is true then normal continuation fallows,else the program will terminate abnormally can rise assertion error.

The fallowing program shows how the assertion error will rise

Example:

class Demo
{
public static void main(String args[])
{
int x=5;
...........
...........
..........
assert(x>10);
System.out.println(x);
}
}

Output:

javac Demo.java
java Demo   //In this case assert statement will not be executed because it is disable by default, so you need to write the below statement

java -ea Demo

Now, it will generates assertion error.

2. Augmented assert:

Syntax: 

assert condition:expr;

In the above syntax expr is a value that is passed to the Assertion Constructor. This value is converted to its string format and displayed if an assertion fails.

Example:

assert e1:e2;
Here,e1 is boolean type and e2 is any thing allowed including method calls also.

class Demo
{
public static void main(String[] args)
{
int a =20 ;

assert(a>20):"here the value of a should be > 20 but it is "+a;
System.out.println(a);
}
}


Output:


 
You must have notice another method will be:

assert e1:e2;

Here, e2 will be executed if and only if 'e1' is false

Example:

class Demo
{
public static void main(String[] args)
{
int a = 10;

assert(a==0):++a;
System.out.println(a);
}
}


Output:

 
 
List of run time flags:

-ea  ----------> To enable assertions in every non-system class that means user defined class

-da   ---------> To disable assertions in non-system class

The fallowing are the Proper use of assertions in real time:

1) It is improper to use assert statement for validating the argument of public method

Example: 

public void withdraw(double amount)
{
assert(amount>=200);
}
 

2) It is improper to use assert statement for validating private method argument
3) It is improper to mix programming language with assert statement
4) In our code if there is any place where the control is not allowed to reach. It is the best place to use assert statement.
            


 

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...