aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Assert " Watch "Regarding Assert " New topic
Author

Regarding Assert

raji alluru
Greenhorn

Joined: Apr 20, 2006
Posts: 5
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

Joined: Feb 24, 2006
Posts: 6657
    
    5

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
Greenhorn

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding Assert
 
Similar Threads
assertion
assertions
Assertion
Assertions Problem
Assertion