#defines and enumerations in Java - How ?

Discussion in 'Java' started by exquisitus, Feb 18, 2005.

  1. exquisitus

    exquisitus Guest

    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
    exquisitus, Feb 18, 2005
    #1
    1. Advertising

  2. exquisitus wrote:

    > 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.

    --
    Cheers
    grundig
    Marcin Grunwald, Feb 18, 2005
    #2
    1. Advertising

  3. Marcin Grunwald wrote:

    > exquisitus wrote:
    >> 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.


    And about enums you can read here:
    http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

    --
    Cheers
    grundig
    Marcin Grunwald, Feb 18, 2005
    #3
  4. exquisitus <> writes:

    > #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) }
    Tor Iver Wilhelmsen, Feb 18, 2005
    #4
  5. exquisitus

    exquisitus Guest

    Thanks guys - for the quick feedback

    exquisitus wrote:

    > 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
    >
    exquisitus, Feb 18, 2005
    #5
  6. exquisitus

    Tilman Bohn Guest

    In message <cv4kpc$8c1$>,
    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 ;-)

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 18, 2005
    #6
  7. exquisitus

    exquisitus Guest

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

    Tilman Bohn wrote:

    > In message <cv4kpc$8c1$>,
    > 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 ;-)
    >
    exquisitus, Feb 18, 2005
    #7
  8. exquisitus

    bugbear Guest

    Tor Iver Wilhelmsen wrote:
    > exquisitus <> writes:
    >
    >
    >>#define TOP_ONE 1 //Constant

    >
    >
    > 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
    bugbear, Feb 18, 2005
    #8
  9. exquisitus

    Tilman Bohn Guest

    In message <cv5506$3kj$>,
    exquisitus wrote on Fri, 18 Feb 2005 16:26:47 +0000 (UTC):

    > 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.

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 19, 2005
    #9
  10. exquisitus

    exquisitus Guest

    Tks for the gory details :)

    Tilman Bohn wrote:
    > In message <cv5506$3kj$>,
    > exquisitus wrote on Fri, 18 Feb 2005 16:26:47 +0000 (UTC):
    >
    >
    >>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.
    >
    exquisitus, Feb 19, 2005
    #10
  11. Tilman Bohn wrote:
    > #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.
    Thomas Schodt, Feb 19, 2005
    #11
  12. exquisitus

    Tilman Bohn Guest

    In message <cv7daf$dr8$>,
    exquisitus wrote on Sat, 19 Feb 2005 13:01:03 +0000 (UTC):

    > Tks for the gory details :)


    Just promise me you won't do it! And if you do, you didn't hear it from
    me! ;-)

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 19, 2005
    #12
  13. exquisitus

    Tilman Bohn Guest

    In message <cv7hbs$73o$1$>,
    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

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 19, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Leonard
    Replies:
    2
    Views:
    375
    Rick Genter
    Sep 28, 2003
  2. Christopher Benson-Manica

    reinterpret_cast and enumerations

    Christopher Benson-Manica, Nov 2, 2004, in forum: C++
    Replies:
    5
    Views:
    818
    Ron Natalie
    Nov 3, 2004
  3. theotyflos
    Replies:
    3
    Views:
    465
    Thomas Matthews
    Feb 19, 2004
  4. Replies:
    1
    Views:
    386
    Ian Collins
    Jun 29, 2006
  5. kasiyil
    Replies:
    3
    Views:
    327
    Jens Theisen
    Aug 21, 2006
Loading...

Share This Page