Re: Singly Linked LIst and Objects Newbie Question
Stefan Ram wrote:
xen <xen@rotmail.nl> writes:
you need multiple statements for to calculate,
so you can't fit it into the assert statement
public class Main
{ public static void main( final java.lang.String[] args )
{ assert new java.util.concurrent.Callable<java.lang.Boolean>()
{ public java.lang.Boolean call()
{ int a = 7;
++a;
return a == 9; }}.call();
java.lang.System.out.println( "ok." ); }}
Exception in thread "main" java.lang.AssertionError
at Main.main(Main.java:3)
You're scaring me.
Actually, I like this idiom a lot, except for what looks suspiciously like
abuse of the purpose of assertions. The use of an anonymous class to provide
closure-like action is a cool, if at first confusing-looking way to do things.
Java programmers must design their assertions with the thought that they will
likely be turned off in production firmly in mind.
Assertions exist primarily to enforce algorithmic invariants, although their
power admits of expanded usage based on that theme. Their highest, best use
is as part of a test strategy and framework.
Assertions are very poorly suited for run-time data validation. The rule of
thumb is to use assertions on private and package-private data and methods;
use conditional branching and exceptions on protected and public data and methods.
Note that failed assertions throw an Error, "a subclass of Throwable that
indicates serious problems that a reasonable application should not try to catch."
<http://java.sun.com/javase/6/docs/api/java/lang/Error.html>
Assertions are much more heavyweight and specialized in purpose than
validation logic and exceptions are.
--
Lew