tags:

views:

143

answers:

7

What does assert do? for example in the function?

private static int charAt(String s, int d) { 

    assert d >= 0 && d <= s.length();

    if (d == s.length()) return -1;

    return s.charAt(d);
}
A: 

It ensures that the expression returns true. Otherwise, it throws a java.lang.AssertionError.

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10

bkail
+1  A: 

assert is a debugging tool that will cause the program to throw an AssertionFailed exception if the condition is not true. In this case, the program will throw an exception if either of the two conditions following it evaluate to false. Generally speaking, assert should not be used in production code

Chris Thompson
It doesn't necessarily cause the program to crash. You can, in fact, catch an `AssertionError`, just like any other exception.
Richard Fearn
And since assertions need to be explicitly enabled to have any effect, having them in production code doesn't hurt.
meriton
Ah my mistake, is it in C then that they are more troublesome? That could be where I'm remembering that paradigm from...
Chris Thompson
Well I must say I agree. Java asserts should not be used in production code. I.e. the client should not be told to launch with the `-ea` switch.
aioobe
+1  A: 

Use this version of the assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message.

The purpose of the detail message is to capture and communicate the details of the assertion failure. The message should allow you to diagnose and ultimately fix the error that led the assertion to fail. Note that the detail message is not a user-level error message, so it is generally unnecessary to make these messages understandable in isolation, or to internationalize them. The detail message is meant to be interpreted in the context of a full stack trace, in conjunction with the source code containing the failed assertion.

JavaDoc

Jan Kuboschek
+3  A: 

If the condition isn't satisfied, an AssertionError will be thrown.

Assertions have to be enabled, though; otherwise the assert expression does nothing. See:

http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html#enable-disable

Richard Fearn
A: 

Assert does throw an AssertionError if you run your app with assertions turned on.

int a = 42;
assert a >= 0 && d <= 10;

If you run this with, say: java -ea -jar peiska.jar

It shall throw an java.lang.AssertionError

NoozNooz42
+2  A: 
aioobe
Agreed, asserts should not be used to ensure argument validity but to point out strong assumptions.
Willi
Using `assert` to test a **non public** method's [precondition](http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html#preconditions) is perfectly valid IMO.
Pascal Thivent
@Pascal Thivent. Good point. Thanks for pointing that out.
aioobe
@aioobe: failed assertions throws `AssertionError`, not `-Exception`.
polygenelubricants
@polygenelubricants, ah, of course, thanks! updated.
aioobe
A: 

Assertions are generally used primarily as a means of checking the program's expected behavior. It should lead to a crash in most cases, since the programmer's assumptions about the state of the program are false. This is where the debugging aspect of assertions come in. They create a checkpoint that we simply can't ignore if we would like to have correct behavior.

In your case it does data validation on the incoming parameters, though it does not prevent clients from misusing the function in the future. Especially if they are not, (and should not) be included in release builds.

zdawg