Assertion:
Assertion is a statement in java. It can be used to test your assumptions about the program.
While executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose.
Advantage of Assertion:
It provides an effective way to detect and correct programming errors.
Simple Example of Assertion in java:
class asrt
{
static int balance=700;
public static int show(int amount)
{
balance=balance-amount;
System.out.println(balance);
return balance;
}
public static void main(String args[])
{
for(int i=0;i<5;i++)
{
int res=show(100);
assert(res>300):"Error";
}
}
}
output :
600
500
400
300
No comments:
Post a Comment