File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Does java supports  conditional compilation ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does java supports  conditional compilation ?" Watch "Does java supports  conditional compilation ?" New topic

Does java supports conditional compilation ?

Ant Swa

Joined: May 28, 2002
Posts: 13
C++ has this powerfull feature of conditional compilation. It enables choosing between different code segments to compile depending on a flag. Does Java has a similar feature. ?
Say, for example, for a paricular program we can compile a debug version with System.out messages. and a production version without the messages if that kind of feature is avalable.
Please enlighten me..

Frank Carver

Joined: Jan 07, 1999
Posts: 6920
There are two answers to this, really.
The first second answer is that, no, java does not have a built-in preprocessor like C and C++ do, so it has not got conditional compilation. There's nothing stopping you running any external preprocessor you like over your code before you compile it, though. Checkout freely available preprocessors such as "m4".
The second answer is that, no, Java does not have a preprocessor, but it does have a pretty smart compiler, and if you do something like:

It probably won't bother generating the code for the print. The compiler knows at compile time that the code can never be executed, because the "if" condition is "final".
Has this helped ?

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ant Swa

Joined: May 28, 2002
Posts: 13
thanks, I got the point.
I agree. Here's the link:
subject: Does java supports conditional compilation ?
It's not a secret anymore!