need help with assert

Hi, somebody could tell me what the "assert" is for... thanks...
[71 byte] By [jorgechavez77a] at [2007-11-27 4:48:26]
# 1
RTFM: http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
fw9189a at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 2
> Hi, somebody could tell me what the "assert" is> for... thanks...It's very seldom used, you probably won't need it unless you are studying for a certification :)
kajbja at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 3

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

nogoodatcodinga at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 4
Can help with unit tests.
kdajania at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 5
Don't confuse the assertXXX methods in the unit test framework with the assert statement.
malcolmmca at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 6
thanks buddy...
jorgechavez77a at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 7
I've actually never used them, I was just making the assumption based on my knowledge of it from C++. Thanks for the clarification though.
kdajania at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 8
> Can help with unit tests.Unit testing and assertions are ultimately different things. I think someone posted the analogy that unit testing was like crash testing cars, and assertions were like seat belts and air bags.
Hippolytea at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 9

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?

kdajania at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 10
Are you describing testing code or live code?
Hippolytea at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 11
This is testing code. That is how I've used assert in the past with C++, but I've never used it with java. I figure since my assumption about in java was wrong, that I ask.
kdajania at 2007-7-12 10:01:19 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

Hippolytea at 2007-7-12 10:01:20 > top of Java-index,Java Essentials,Java Programming...
# 13
Got it. Thanks for the info.
kdajania at 2007-7-12 10:01:20 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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.

jverda at 2007-7-12 10:01:20 > top of Java-index,Java Essentials,Java Programming...