• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Assert

 
raji alluru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AssertionTest

{
public void methodA(int i)
{
assert i >= 0 : methodB();
System.out.println(i);
}

public String methodB()
{
System.out.println("jfgjk");
return "The value must not be negative";
}

public static void main(String args[])
{
AssertionTest test = new AssertionTest();
test.methodA(-10);
}
}

In the above program when it will print -"The value must not be negative"...
Please give me proper example about how assert is useful....
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you use the -ea option when you tried to run this program ?

java -ea AssertionTest

This will enable assertions and result in an assertion error in your program. Assertions are available from java version 1.4 and above.
 
raji alluru
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i got the output as which i required by putting -ea...Thanks
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure in the exam for questions regarding the usage of assertion to validate the arguments of a public method;
You can validate the parameters of public method by assertion but SUN recommends you better not do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic