Assertions in java help us in testing our assumptions during development. The assertion code gets erased when the programme is deployed. While we are developing an application we use 'System.out.println()' in a java class for debugging purpose. We use it inorder to find out any logical problems. Instead we can use Assertions concept.
Ex:
public int myAccNum(int accno)
{
assert(accno > 0);// line 1
int myAccno=accno;
}
If the accno is negative on line 1 then the compiler throws an assertion error.
Assertions needed to be turned on then they will work. Other wise the compiler will overlook the Assertion code. There are two ways to write assertion code.
1. assert( X > A);
2. assert(X > A) : "X value is" + X + "A value is" + A
java -ea com.venkates.TestClass
java -enableassertions com.venkates.Testclass
The above two commands enable assertions at runtime. The below commands disable assertions .
java -da com.venkates.TestClass
java -disableassertions com.venkates.TestClass
Ex:
public int myAccNum(int accno)
{
assert(accno > 0);// line 1
int myAccno=accno;
}
If the accno is negative on line 1 then the compiler throws an assertion error.
Assertions needed to be turned on then they will work. Other wise the compiler will overlook the Assertion code. There are two ways to write assertion code.
1. assert( X > A);
2. assert(X > A) : "X value is" + X + "A value is" + A
java -ea com.venkates.TestClass
java -enableassertions com.venkates.Testclass
The above two commands enable assertions at runtime. The below commands disable assertions .
java -da com.venkates.TestClass
java -disableassertions com.venkates.TestClass
No comments:
Post a Comment