img
if(n < 0) {
System.out.println("n is negative!");
return; // or throw an exception
}
With assert, you need only one line of code. Furthermore, you don't have to remove the
assert statements from your released code.
Assertion Enabling and Disabling Options
When executing code, you can disable assertions by using the -da option. You can enable or
disable a specific package by specifying its name after the -ea or -da option. For example, to
enable assertions in a package called MyPack, use
-ea:MyPack
To disable assertions in MyPack, use
-da:MyPack
To enable or disable all subpackages of a package, follow the package name with three dots.
For example,
-ea:MyPack...
You can also specify a class with the -ea or -da option. For example, this enables
AssertDemo individually:
-ea:AssertDemo
Static Import
JDK 5 added a new feature to Java called static import that expands the capabilities of the
import keyword. By following import with the keyword static, an import statement can
be used to import the static members of a class or interface. When using static import, it is
possible to refer to static members directly by their names, without having to qualify them
with the name of their class. This simplifies and shortens the syntax required to use a static
member.
To understand the usefulness of static import, let's begin with an example that does
not use it. The following program computes the hypotenuse of a right triangle. It uses two
static methods from Java's built-in math class Math, which is part of java.lang. The first is
Math.pow( ), which returns a value raised to a specified power. The second is Math.sqrt( ),
which returns the square root of its argument.
// Compute the hypotenuse of a right triangle.
class Hypot {
public static void main(String args[]) {
double side1, side2;
double hypot;
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home