Conditional compilation could be defined as ôa process where selected parts of a program are included or excluded from the compilation of an application, based on a set of developer-defined conditions.ö The two mechanisms Visual J++ provides for Conditional compilation are conditional compiler directives (conditional directives,) and conditional methods.
Conditional methods are useful for
Visual J++ allows you to select relies on the use of a preprocessor. A preprocessor is typically a component of a language compiler that reads in a source file and makes certain changes in order to prepare the source code for compilation. In a program written with Visual J++, the preprocessor examines the Java source code and looks for certain directives, all beginning with the pound sign symbol (#), evaluates the lines according to the information found, then performs such operations as extracting lines, replacing values, and emitting error and warning messages. After the preprocessor completes its work, the alter source code is then checked for syntactical errors and finally compiled into bytecode.
The conditional compilation mechanism in Visual J++ is an extension to the Java language, provided by Microsoft. Thus, source code containing these directives will not compile properly with other tools.
Conditional compilation is an extremely useful tool that aids software development. For example, you can use conditional compilation mechanisms to establish organization-wide coding standards, or you can use them to create helpful, ongoing debugging processes during the development process. In the later case, with just a few minor alterations, the code can be ready to shipped to customers without debugging information.
Other reasons to use conditional compilation include:
Following is an example of compiler directives and conditional methods used in a Java application.
#define DEBUG // WeÆre debugging the application now
class printIt {
//* @conditional(debug) */
public void assert( ) {
System.out.println(ôHello World û From the assert methodö);
}
}
public class myApp (
}
Before you start using these elements in your own code, it will be helpful to review some of the rules governing their use. After youÆre familiar with the guidelines, take a look at the directives available and their specific uses.
Visual J++ recognizes the following conditional directives:
#if | #elif | #else | #endif |
#define | #undef | #error | #warning |