What is the java equivalent to #define

Hi, What would this declaration be in java:#defineSTRAIGHT_FLUSH1Many thanks, Ron
[109 byte] By [cakea] at [2007-11-26 19:44:12]
# 1
<shudders/>
DrLaszloJamfa at 2007-7-9 22:28:09 > top of Java-index,Java Essentials,New To Java...
# 2
Sorry about that -- it happens whenever anything reminds me of C.I suggest you check out enums: http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
DrLaszloJamfa at 2007-7-9 22:28:09 > top of Java-index,Java Essentials,New To Java...
# 3

There is no direct equivalent. As mentioned, enums can be used to fill one of #define's roles. (enumerated values)

public static final constant member variables can fulfill another. (named constants)

And actually, if I understand correctly, #define is not part of the C language per se, but belongs in the realm of the preprocessor, and you can run Java code through a c preprocessor.

EDIT: Yeah, you can:

:; cat X.java

#define blah int

public class X {

Y y;

blah x;

}

jeff@shiro:/tmp 14:33:34

:; cpp -P !$

cpp -P X.java

public class X {

Y y;

int x;

}

Message was edited by:

jverd

jverda at 2007-7-9 22:28:09 > top of Java-index,Java Essentials,New To Java...
# 4

> What would this declaration be in java:

>

> #defineSTRAIGHT_FLUSH1

You would declare,

public static final int STRAIGHT_FLUSH = 1;

in a class. Or you could be more OO and declare an enum, like

public enum Hands {STRAIGHT_FLUSH, PAIR, FLUSH /* etcetera */}

jverda at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 5
Ok, found it, Google 'is' my friend :)For anybody else interested: http://java.sun.com/developer/JDCTechTips/2003/tt0408.htmlThanks, Ron
cakea at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 6
I can't believe you were all so quick, lol, thanks for all the replies...Ron
cakea at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 7
> http://java.sun.com/developer/JDCTechTips/2003/tt0408.htmlThat reference dates back to 1.4. enums were introduced in 1.5, so that is why they are not mentioned there.
DrLaszloJamfa at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 8
Hi,So this one I can't work out:#defineRANK(x)((x >> 8) & 0xF)Many thanks, Ron
cakea at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 9
What problem are you having? It seems to work for me.More importantly, though: Why do you want to do this? What benefit do you think you're getting?
jverda at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 10

> #defineRANK(x)((x >> 8) & 0xF)

Are you translating C into Java? (I think I just spit up in my mouth a little.)

You will have to turn that into a method or two:

public static int rank(int x) {

return (x>>8) & 0xf;

}

You may want to have versions that take x's of type other than int -- long?

And I'm guessing about the returm type. Return type byte could be

more appropriate, for example.

DrLaszloJamfa at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 11

Dr - yes I am, I am just mucking around with a couple of card games... I was implementing something that I had written, and then of course ran across something that was much more efficient but only available in c so I thought I would have a go at converting it... you know, just to see, and I wanted to see how much faster and more efficient it was... haha

cakea at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...
# 12
Okay, but it's hard to imagine how efficiency would be at all relevant,unless it's applying some exhaustive algorithms to playing the card games itself.
DrLaszloJamfa at 2007-7-9 22:28:10 > top of Java-index,Java Essentials,New To Java...