|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|
Assertion in SCJP 1.4 & SCJP 5.0
The assertion facility provides a mechanism for adding optional “sanity checks” to
your code. These checks are used during the development and testing phases, but
are turned off when the software is deployed. This allows the programmer to insert
debugging checks that might be too slow or memory-intensive to use in a real con-text,
but that help during development. In a sense, assertions are a lot like error
checks, except that they are turned off for deployment.
Assertions generally are implemented in such a way that they can be compiled
out; in languages like C and C++, this means using the preprocessor. Since Java
doesn’t have macro facilities, features that otherwise might be created by the pro-grammer
must be built into the language themselves. As a result, assertions have
not been used widely in Java. The 1.4 release of the JDK corrects this.
One of the most important features of this facility is that these checks can be
turned on and off at runtime, which means that you don’t have to decide during
development whether or not these checks should remain in the code or be removed
before deployment.
Assertion basics
An assertion is a conditional expression that should evaluate to true if and only if
your code is working correctly. If the expression evaluates to false, an error is sig-naled.
Here is an example of an assertion (shown in bold):
public class aClass {
public void aMethod( int argument ) {
Foo foo = null;
// ... somehow get a Foo object
// Now check to make sure we've managed to get one:
assert foo != null;
}
}
This asserts that foo is not null. If foo is in fact null, an AssertionError is thrown.
Any code that executes after this line can safely assume that foo is not null.
Assertions are very simple, but we’ll be looking at their usage in detail because
assertions are very important in the quest for robust code. In this chapter, we’ll
learn not only how to use assertions, but when and where to use them.
Why use assertions?
It is widely acknowledged in programming circles that software isn’t stable enough.
We all know we’re not doing enough error-checking. It is also acknowledged that
error-handling comprises a substantial amount of programming effort and a sub-stantial
portion of the resulting code. Error-handling code is also relatively dull to
write, especially compared with the main algorithm whose errors are being handled.
Furthermore, dealing with errors can sometimes force you to consider design
questions that you may be trying to avoid. In such situations, programmers gener-ally
just ignore the possibility of error, mostly because they don’t want to lose their
train of thought.
As programmers, we need to do more error-checking, and assertions are an
important step in this direction. Assertions can be used to catch conditions that we
don’t expect to happen. This may sound paradoxical, but given that we rarely check
for enough errors, it makes a certain sense. For every error we think of while pro-gramming,
there are a whole bunch more that never occur to us. It’s not possible to
eliminate all errors, but we can plan ahead for the unexpected.
|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|