Wednesday, March 11, 2009

Example code assertion




Demo 1:
public class Aser1 {

public static void main(String arg[])
{
int x=10;
assert (x==10); // valid
System.out.println(x);
}
}

For Run

java -ea Ase1
output: 10

If we write like this assert (x>10);
java -ea Ase1
output:
Assertion error

Demo 2:
public class Aser2 {

public static void main(String sr[])
{
int x=10;
assert(x>10):m1();
System.out.println(x);
}
public static int m1()
{
return 1000;
}
}

For Run

java -ea Aser2
output:
Assertion error:1000



0 comments:

Post a Comment