Assert is basically to make sure that some assumption that you made while writing some code holds true.
For example if you have a method where you assume that you'll never get a negative value and you write your code accordingly. But you still want to check for that condition. Adding the check using a try-catch block or if-else statements involves overhead which would slow down and increase the size of your app.
Assertions allow a way around this.
private void methodA(int num) {
if (num >= 0) {
// do stuff
} else { // num must be < 0
// This code will never be reached!
System.out.println("Yikes! num is a negative number! " + num);
}
useNum(num + x);
}
Instead of this clumsy code, you can use:
private void methodA(int num) {
assert (num>=0); // throws an AssertionError
// if this test isn't true
useNum(num + x);
}
Other than being neater and faster, the advantage is that your code runs like this:
private void methodA(int num) {
useNum(num + x); // we've tested this;
// we now know we're good here
}
There is also a second form of assert that allows you to add more information to the stack trace:
private void doStuff() {
assert (y > x): "y is " + y " " x is " + x;
// more code assuming y is greater than x
}
The catch is this: you have to specifically enable assertion code while testing and before production. Assertions are off by default. With 1.4, assert is a keyword but prior to that it could be used as either a keyword or as an identifier!
There's loads more to assertions; how to selectively enable for classes, packages etc.; how to compile with assertions for code that's from 1.3 etc.
What you should keep in mind is this:
You should not try to catch assertion failure. It's meant to cause a stop.
You should not use assertions for arguments of public methods since they can be called by objects and methods apart from your own and you won't have control on those.
You should not use assertion expressions that will cause side-effects; that is, don't use:
public void doStuff() {
assert (modifyThings());
// continues on
}
public boolean modifyThings() {
x++ = y;
return true;
}
since the assertion will modiy the value of x
You should use assert for validating stuff that should never happen; like maybe reaching the default argument of a switch-case and you should definitely use assertions for private methods since only your code calls them and you should verify you get correct arguments.
(Code from Kathy Sierra and Bert Bates' Sun Certified Programmer and Developer for Java2
I like that analogy. But let me ask you this...
Let's say you have a class that runs through a mathematical process over time on an image in chunks or lines of the image, and you want to compare the end result to the end result of a class that does the same mathematical process on the image, but with one full pass on the image. You do this to make sure that the process running on lines of the image after a a certain period of time will come up with the same result doing it on the whole thing at once. Is that a valid case to use assertions in java?
Have you checked out JUnit: http://www.junit.org/index.htm
If you are using that framework, or some competing one, your testing code
will call specific methods in framework classes, like assertEquals() or fail().
These frameworks don't reply on the Java language's assert statement.
> Assert is basically to make sure that some assumption
> that you made while writing some code holds true.
>
> For example if you have a method where you assume
> that you'll never get a negative value and you write
> your code accordingly. But you still want to check
> for that condition. Adding the check using a
> try-catch block or if-else statements involves
> overhead which would slow down and increase the size
> of your app.
>
> Assertions allow a way around this.
>
> private void methodA(int num) {
> if (num >= 0) {
> // do stuff
> } else { // num must be < 0
> // This code will never be reached!
> System.out.println("Yikes! num is a negative number!
> " + num);
> }
> useNum(num + x);
> }
>
> Instead of this clumsy code, you can use:
>
> private void methodA(int num) {
> assert (num>=0); // throws an AssertionError
> // if this test isn't true
> useNum(num + x);
> }
No, absolutely not.
Do NOT use the assert keyword as a replacement for exceptions that your program depends on for correct operation. Assert is just a sanity check. It's a development/debugging tool, and it can be turned off at runtime.