#defines and enumerations in Java - How ?

E

exquisitus

I have C code that I am converting to Java.

I have a few #defines of different types (example):

#define TOP_ONE 1 //Constant
#define max(x,y) ((x) > (y)) ? (x):(y) //Macro definition

I also have some enumerations

typedef enum
{
x = 1000,
y = 1001
} myTag ;

Any suggestions as to the best way to convert these to Java. I remeber
reading somewhere that there was a gotcha somewhere when enums are
defined as static finals.

Any help appreciated. tkx
 
M

Marcin Grunwald

exquisitus said:
I have C code that I am converting to Java.

I have a few #defines of different types (example):

#define TOP_ONE 1 //Constant
#define max(x,y) ((x) > (y)) ? (x):(y) //Macro definition

I also have some enumerations

typedef enum
{
x = 1000,
y = 1001
} myTag ;

Any suggestions as to the best way to convert these to Java. I remeber
reading somewhere that there was a gotcha somewhere when enums are
defined as static finals.

Any help appreciated. tkx

Java hasn't #define like in C.

You can define constant, for example:
public static final int TOP_ONE 1;

Macro definition you can substitute only with method.
 
T

Tor Iver Wilhelmsen

exquisitus said:
#define TOP_ONE 1 //Constant

public static final int TOP_ONE = 1;

Note that "constants" in Java are typed whereas precompiler defines
are not.
#define max(x,y) ((x) > (y)) ? (x):(y) //Macro definition

No macros, use a method instead: Declare it final and assume it will
be inlined by the compiler.
typedef enum
{
x = 1000,
y = 1001
} myTag ;

Enums are supported in Java 1.5.x aka. 5.0. Not the "typedef" keyword,
though, but it's not needed.

public enum myTag { x (1000), y (1001) }
 
T

Tilman Bohn

I have C code that I am converting to Java.

I have a few #defines of different types (example):

#define TOP_ONE 1 //Constant
#define max(x,y) ((x) > (y)) ? (x):(y) //Macro definition
[...]

You've gotten some replies already which are completely valid. I just
wanted to mention that there's nothing _in principle_ that keeps you
from running cpp or some other preprocessor (m4,...) on your java source
code. I am certainly _not_ advocating such practice (at least not for
this purpose), but for completeness' sake I think this should be
mentioned.

cpp -P -D"private=public" SomeClass.java | less ;-)
 
E

exquisitus

Ok, my interest has been piqued. Tell me more ...

Tilman said:
In message <[email protected]>,
exquisitus wrote on Fri, 18 Feb 2005 11:50:04 +0000 (UTC):

I have C code that I am converting to Java.

I have a few #defines of different types (example):

#define TOP_ONE 1 //Constant
#define max(x,y) ((x) > (y)) ? (x):(y) //Macro definition

[...]

You've gotten some replies already which are completely valid. I just
wanted to mention that there's nothing _in principle_ that keeps you
from running cpp or some other preprocessor (m4,...) on your java source
code. I am certainly _not_ advocating such practice (at least not for
this purpose), but for completeness' sake I think this should be
mentioned.

cpp -P -D"private=public" SomeClass.java | less ;-)
 
B

bugbear

Tor said:
public static final int TOP_ONE = 1;

Note that "constants" in Java are typed whereas precompiler defines
are not.

The nice thing about preprocessors is you can be as
"proper" as you like

#define TOP_ONE ((int)1)

(or as horrid, of course ;-)

BugBear
 
T

Tilman Bohn

Ok, my interest has been piqued. Tell me more ...

#define's, #include's and #ifdef's in C/C++ are never seen by the
compiler, but are exclusively and completely handled by the preprocessor.
The compiler only ever sees the preprocessor output. However, the
preprocessor doesn't really care whether it's working on C or Java source
code. In this respect it is language agnostic and just does its purely
textual replacements. (Although cpp does have knowledge of C syntax or
other details for other purposes, but that's not relevant here. It can be
used in an entirely C-agnostic way.)

Saying #define MY_CONST 1 at the beginning of your java source file and
running it through cpp would simply replace all occurrences of `MY_CONST'
by `1', just as it would if it were a C source file. (Do note that this
will not buy you anything over declaring it as static final, as javac will
inline that on its own.) The same would hold for using some other kind of
preprocessor like m4. As I said, I don't recommend this (in fact I
strongly advise against it!) for your purposes; it would be a bad bad hack
without any advantages over the correct ways to handle the stuff you
asked. But it is theoretically possible. And there could be certain code
generation scenarios where it would be an arguably acceptable technique.

My post was really just meant as one of those `don't try this at home
kids' things.
 
E

exquisitus

Tks for the gory details :)

Tilman said:
In message <[email protected]>,
exquisitus wrote on Fri, 18 Feb 2005 16:26:47 +0000 (UTC):




#define's, #include's and #ifdef's in C/C++ are never seen by the
compiler, but are exclusively and completely handled by the preprocessor.
The compiler only ever sees the preprocessor output. However, the
preprocessor doesn't really care whether it's working on C or Java source
code. In this respect it is language agnostic and just does its purely
textual replacements. (Although cpp does have knowledge of C syntax or
other details for other purposes, but that's not relevant here. It can be
used in an entirely C-agnostic way.)

Saying #define MY_CONST 1 at the beginning of your java source file and
running it through cpp would simply replace all occurrences of `MY_CONST'
by `1', just as it would if it were a C source file. (Do note that this
will not buy you anything over declaring it as static final, as javac will
inline that on its own.) The same would hold for using some other kind of
preprocessor like m4. As I said, I don't recommend this (in fact I
strongly advise against it!) for your purposes; it would be a bad bad hack
without any advantages over the correct ways to handle the stuff you
asked. But it is theoretically possible. And there could be certain code
generation scenarios where it would be an arguably acceptable technique.

My post was really just meant as one of those `don't try this at home
kids' things.
 
T

Thomas Schodt

Tilman said:
#define's, #include's and #ifdef's in C/C++ are never seen by the
compiler, but are exclusively and completely handled by the preprocessor.
The compiler only ever sees the preprocessor output.

This is kind of similar to the Java "preprocessing" of \uNNNN,
javac only sees the source code after substitution
which can cause some tricky compiler messages
if you put \u000d or \u000a in string literals or // comments.
 
T

Tilman Bohn

In message <[email protected]>,
Thomas Schodt wrote on Sat, 19 Feb 2005 14:10:02 +0000:

[...]
This is kind of similar to the Java "preprocessing" of \uNNNN,
javac only sees the source code after substitution
which can cause some tricky compiler messages
if you put \u000d or \u000a in string literals or // comments.

Peter van der Linden's classic:

/* Just Java
Peter van der Linden
April 1, 1996.

\u0050\u0076\u0064\u004c\u0020\u0031\u0020\u0041\u0070\u0072\u0039\u0036
\u002a\u002f\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0068\u0020\u007b
\u0020\u0020\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020
\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0020\u0076\u006f\u0069\u0064
\u006d\u0061\u0069\u006e\u0028\u0020\u0053\u0074\u0072\u0069\u006e\u0067
\u005b\u005d\u0061\u0029\u0020\u007b\u0053\u0079\u0073\u0074\u0065\u006d
\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e
\u0028\u0022\u0048\u0069\u0021\u0022\u0029\u003b\u007d\u007d\u002f\u002a

*/

javac h.java; java h
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top