SummaryHow to get the best benefits of at least some of the C / C textual preprocessor's features in Java:. Constants and conditional compilation (800 words) benefits of using the C preprocessor's facilities to define compile-time constants and conditionally compiled code.
. Java has gotten rid of the entire notion of a textual preprocessor (if you take Java as a "descendent" of C / C ) We can, however, get the best benefits of at least some of the C preprocessor's features in Java: constants And Conditional CompiLation.
One of the inarguably good features of the C preprocessor is the ability to define compile-time constants using a textual name to represent some value. This makes it easier to read and maintain. It is also faster at runtime than using a normal variable.
An arguably abused feature of the C preprocessor is the use of #define along with #ifdef and friends to conditionally compile entire blocks of code. I say it's arguable since people often use this facility to deal with platform-specific issues (and that's both the Good point and the bad point.
IN C, One Could Define Some Constants in A Header File Via:
#define my_bdate 10 # Define Silly_Platform
And the getting access to those constants by using #include to incrude the in a code file, and then using them:
FPRINTF (stderr, "my birthday is on the% D" "TH! / N", my_bdate);
The Equivalent in Java Can Be Done by Creating Public Static Final Variables in a java interface:
Interface constantstuff {public static final int my_bdate = 10; public static final boolean sillyplatform = true;
THEN We can access the interface visible to us and then using the constants: system.out.println ("my birthday is on the" constantstuff.my_bdate "t!");
The C Preprocessor CAN CONDITIONALLY STRIP OUT LARGE AREAS OF TEXT IF A Given Preprocessor Constant Was Or Was Not Defeed.
#if Defined (Silly_Platform) / * Lot's of Nasty Code to DEAL with the stupidities of the * silly platform. * / # else / * code to deal with ost, normal platforms. * / # Endif
Many folks lament that this capability is absent from Java. Remember, one of the reasons that Java is so wonderful is that the language is so much better defined, so system-specific code like that should not even be necessary.
Be that as it may, you can still get that sort of conditionally compiled code from the compiler directly! You just use public static final boolean constants as the condition for a regular if statement. The Java compiler is smart enough to recognize that as a special Case and it can complely eliminated the test and the code of the appropriate conditional branch.
So Just Write The Conditional Statement As UsuAL.
IF (constantstuff.sillyplatform) {// code to be used if platform is true * at compile time *.} else {// code to be useful place time *.
I do not know about you, but I hate having to write that long-winded interface name before using any of those constants. So, I just have my class that is going to use those constants implement the interface. Then I can just use The name Directly, Assuming There area No name clashes (in which case you'll has to distinguish the ".