#define in Java

Matt Gerrans mgerrans at mindspring.com
Thu Aug 15 17:10:21 CEST 2002


> 1. #define is not a variable, it's a preprocessor directive.

I think he really meant "How can I have a #defined variable in Java that
works like C/C++?"

> 2. comp.lang.python is not an appropriate forum in which to ask this
> question.

Actually, maybe it is; since Java doesn't have preprocessing, if you wanted
to add it, using Python might be the easiest way.  However, the fact that
Java doesn't have preprocessing isn't an oversight, it is intentional.  I
personally hate #defines and macros because they really make a mess out of
C/C++ code and make it harder to debug.   But, if you insist on adding
C/C++'s obfuscating technology to Java, writing the preprocessor in Python
would be a snap (although, in keeping with the spirit of the project, you
might prefer Perl).

Python is great for code generation (in any language), too!

> > How to have a #define variable in Java that work like C/C++??

Why?!?   And what do you mean by "works like?"   Do you mean you want a
variable you can't see in the debugger?   Whose type is very difficult to
determine?    One which will give you compiler errors with line numbers from
a file you didn't even know you were compiling?   One that will have strange
side effects that you may never understand?    One which compiles as a
variable one day and an expression the next?   Etc.

Okay, enough pontification, already;  If you are looking for const, then in
Java you'd just use final, or static final.   If you really want conditional
preprocessors, search google (I remember seeing a page for a project that
added preprocessor directives to Java, I don't remember if that was on
sourceforge, or elsewhere).   Projects like Parc's aspect oriented
programming pre-process the Java to add their tricks.

If you are trying to avoid runtime checks (such as a platform-specific
behavior), because they supposedly hurt performance, then I would do the
following: get the code working with runtime checks, then do some profiling
and discover that these checks really don't affect performance at all.  Then
you are finished ahead of schedule.





More information about the Python-list mailing list