Java Reference
In-Depth Information
Here, condition is an expression that must evaluate to a Boolean result. If the result is true,
then the assertion is true and no other action takes place. If the condition is false, then the
assertion fails and a default AssertionError object is thrown. For example,
If n is less than or equal to zero, then an AssertionError is thrown. Otherwise, no action
takes place.
The second form of assert is shown here:
assert condition : expr ;
In this version, expr is a value that is passed to the AssertionError constructor. This value
is converted to its string format and displayed if an assertion fails. Typically, you will spe-
cify a string for expr , but any non- void expression is allowed as long as it defines a reason-
able string conversion.
To enable assertion checking at run time, you must specify the -ea option. For example,
to enable assertions for Sample , execute it using this line:
Assertions are quite useful during development because they streamline the type of error
checking that is common during testing. But be careful—you must not rely on an assertion
to perform any action actually required by the program. The reason is that normally, re-
leased code will be run with assertions disabled and the expression in an assertion will not
be evaluated.
Native Methods
Although rare, there may occasionally be times when you will want to call a subroutine that
is written in a language other than Java. Typically, such a subroutine will exist as execut-
able code for the CPU and environment in which you are working—that is, native code. For
example, you may wish to call a native code subroutine in order to achieve faster execution
time. Or you may want to use a specialized, third-party library, such as a statistical pack-
age. However, since Java programs are compiled to bytecode, which is then interpreted (or
compiled on the fly) by the Java run-time system, it would seem impossible to call a native
code subroutine from within your Java program. Fortunately, this conclusion is false. Java
provides the native keyword, which is used to declare native code methods. Once declared,
these methods can be called from inside your Java program just as you call any other Java
method.
Search WWH ::




Custom Search