One feature of
Java has been canceled is "conditional compilation" of C, which allows us to change parameters, get different behaviors, and not change any other code. Java's abandonment of this feature may be due to the fact that the feature is often used in C to solve cross-platform problems: Different parts of the code are compiled on a specific platform and cannot be run on a particular platform. Because the Java design idea is to become an automatic cross-platform language, so this feature is not necessary.
However, conditional compilation has other very valuable uses. A very common use is debugging code. Debugging features can be used during development, but not in products that are released. Alen Holub (www.holub.com) proposes the concept of using package (package) to simulate conditional compilation. Based on this concept, it creates a very useful Java version of the C "assertion mechanism". It is called a "determination mechanism" because we can say "it should be true" or "it should be false." If the statement does not agree with your assertion, you can find the relevant situation. This tool is particularly useful during debugging.
The following class can be used for program debugging:
: Assert.java
//Assertion tool for debugging
package com.bruceeckel.tools.debug;
public class Assert {
private static void Perr (String msg) {
System.err.println (msg);
}
Public final static void Is_true (Boolean exp) {
if (!exp) Perr ("Assertion failed");
Public final static void Is_false (Boolean exp) {
if (exp) perr ("Assertion failed");
}
Public final static void
is_true (boolean exp, String msg) {
if (!exp) Perr ("Assertion failed:" + msg);
} Public
final static void
Is_false (boolean exp, String msg) {
if (exp) perr ("Assertion failed:" + msg);
}
} ///:~
This class simply encapsulates the Boolean test. If it fails, an error message is displayed. In the 9th chapter, you will also learn a more advanced error control tool called "illegal control". But in this case, the Perr () method can work well.
If you want to use this class, you can add the following line to your program:
Import com.bruceeckel.tools.debug.*;
To clear the assertion mechanism so that you can release the final code, we create a second assert class, but in a different package:
: Assert.java
//Turning off the assertion output/I can ship the program
.
Package com.bruceeckel.tools;
public class Assert {public
final static void Is_true (Boolean exp) {} public
final static void Is_false (Boolean ex p) {} public
final static void
is_true (boolean exp, String msg) {} Public
final static void
Is_false ( Boolean exp, String msg) {}
}///:~
Now, if you turn the previous import statement into the following:
Import com.bruceeckel.tools.*;
The program no longer displays assertions. Here's an example:
: Testassert.java
//Demonstrating the Assertion tool
package c05;
Comment the following, and uncomment the
//subsequent line to change assertion behavior:
import Com.bruceeckel . tools.debug.*;
Import com.bruceeckel.tools.*;
public class Testassert {public
static void Main (string[] args) {
assert.is_true (2 + 2) = 5);
Assert.is_false ((1 + 1) = = 2);
Assert.is_true ((2 + 2) = = 5, "2 + 2 = 5");
Assert.is_false ((1 + 1) = = 2, "1 +1!= 2");
}
///:~
By changing the imported package, we can change our code from debug version to final release version. This technique can be applied to any kind of conditional code.