File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Assert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Assert " Watch "Regarding Assert " New topic

Regarding Assert

raji alluru

Joined: Apr 20, 2006
Posts: 5
public class AssertionTest

public void methodA(int i)
assert i >= 0 : methodB();

public String methodB()
return "The value must not be negative";

public static void main(String args[])
AssertionTest test = new AssertionTest();

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

Joined: Feb 24, 2006
Posts: 6662

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.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
raji alluru

Joined: Apr 20, 2006
Posts: 5
yes i got the output as which i required by putting -ea...Thanks
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
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.
I agree. Here's the link:
subject: Regarding Assert
It's not a secret anymore!