DEFINE is actually part of the C preprocessor. I suppose you could run java code through the preprocessor if you wanted to. Never actually tried it. That way, you could have exactly DEFINE.
To declare a constant, one uses public static final <type> <identifier> = <value>;
in java.
There really aren't macros in java. You'd just define a method.
> > Like the ones in C?
>
> In C++ preprocessor usage is discouraged and in Java
> there isn't any. What do you want to accomplish? I'm
> sure there's a way in Java.
The only possibility that I have seen is where there is OS specific java code or even java version specific code particularily code that has a high impact on performance.
In this case one wants the optimal solution for the targetted OS and java version.
Dynamic java solutions can solve it but as stated it is already known that it has a high performance impact so that is less than optimal.
One can also solve it using multiple versions of the same source and with a build system set up to use the correct source. Of course the build system would have to deal with the preprocessor version in some way as well.
But dealing with the multiple source files can be a problem in a build system. And this is particularly true with the common "take all" idiom that Ant encourages.
You'd normally do it by defining an interface to the functionality and supplly platform specific
implementations of the code. At run time this does not have much performance impact (if you are using recent VM's)
If by highly optimal you mean dropping to assembler or routines designed to take advantage of specific
hardware features then you are probably using the wrong language. This kind of thing should probably be
done using JNI where you do have the ability to use #defs and write code targeted to a platform rather
then writing code that is targeted to a VM.
Cheers
Matfud
> You'd normally do it by defining an interface to the
> functionality and supplly platform specific
> implementations of the code. At run time this does not
> have much performance impact (if you are using recent
> VM's)
>
In a method that has a lot of impact, "not much" can still be significant.
> If by highly optimal you mean dropping to assembler or
> routines designed to take advantage of specific
> hardware features then you are probably using the
> wrong language. This kind of thing should probably be
> done using JNI where you do have the ability to use
> #defs and write code targeted to a platform rather
> then writing code that is targeted to a VM.
>
No that isn't what I had in mind.
> > You'd normally do it by defining an interface to the
> > functionality and supplly platform specific
> > implementations of the code. At run time this does
> not
> > have much performance impact (if you are using
> recent
> > VM's)
> >
>
> In a method that has a lot of impact, "not much" can
> still be significant.
If you are careful there will be no performance impact. If you
concentrate on things like marking your implementaions final
and the keeping the class hierarcy fairly flat, then high usage
code should be "pulled up" (or in C parlance, inlined) by the
JIT compiler and remove all the indirection implied by the
class hierarchy/abstraction.
I only said "not much" because, this being java, you cannot
guarentee what VM the code will run on. If it is run on a VM
that chooses slightly different criteria for inlining code, or does
not ever inline then the performance may be degraded. If you
run on a fully interpreted VM then everything is likely to be
slow.
But them's the breaks from using a portable language. The
byte code runs on an abstract machine so performance can never be guarenteed.
If the VM is implemented in hardware, for example, then it is will be very efficient at executing generic bytecode but is
highly unlikely to perform optimisations on the code at runtime so the interface/implementation structure described above would have a performance impact on such a machine.
matfud
> > > You'd normally do it by defining an interface to the
> > > functionality and supplly platform specific
> > > implementations of the code. At run time this does not
> > > have much performance impact (if you are using
> > recent
> > > VM's)
> > >
> >
> > In a method that has a lot of impact, "not much" can
> > still be significant.
>
> If you are careful there will be no performance
> impact. If you
> concentrate on things like marking your implementaions final
> and the keeping the class hierarcy fairly flat, then high usage
> code should be "pulled up" (or in C parlance, inlined) by the
> JIT compiler and remove all the indirection implied by the
> class hierarchy/abstraction.
Cool, but that has nothing to do with what I said.
What I said was that you have the following situation.
1. The application is complete
2. Automated profiling has determined a high impact method.
3. Automated profiling has determined that different optimization strategies are needed to get the best performance from the method on different OSes/VM versions.
4.(And just to make it clear I will add the following constraint) Automated profiling has determined that dynamic loading stragies still impact the application.
I am not claiming that this happens often. It might be that it never happens. But if it does then one might want condersider conditional build strategies (which can be implemented in a variety of ways.)
> See i know there are no macros in Java ....in c++ i
> have code which looks like this:
>
> #define OID = 1,3,6,1,2,1; //This is not a array
>
> ifi have to write the similar macro in java how
> should i do it ....please suggest
No need to re-answer in this thread. This question was re-posted in a new topic here: http://forum.java.sun.com/thread.jspa?threadID=766762
No fights ....here but let me clear on this part that u also dont know much of Java...since how long have u been working on Java? ..a person who deosnt know the answer completely.....even i knew the thing that there is no macro definition in java ...I was asking about whats the alternative ..which thankfully oither people have suggested......NOT U
so now u call me thick head or urself its upto u..... ;-)
> No fights ....here but let me clear on this part that
> u also dont know much of Java...since how long have u
> been working on Java? ..a person who deosnt know the
> answer completely.....even i knew the thing that
> there is no macro definition in java ...I was asking
> about whats the alternative ..which thankfully oither
> people have suggested......NOT U
>
> so now u call me thick head or urself its upto u.....
> ;-)
Please stop using short form for words when trying to insult someone so that there is a chance that people may take your comments seriously. I wouldn't put much on that happening though.
> No fights ....here but let me clear on this part that
> u also dont know much of Java...since how long have u
> been working on Java? ..a person who deosnt know the
> answer completely.....even i knew the thing that
> there is no macro definition in java ...I was asking
> about whats the alternative ..which thankfully oither
> people have suggested......NOT U
>
> so now u call me thick head or urself its upto u.....
> ;-)
LOL...you suck at life?
> See i know there are no macros in Java ....in c++ i
> have code which looks like this:
>
> #define OID = 1,3,6,1,2,1; //This is not a array
>
> ifi have to write the similar macro in java how
> should i do it ....please suggest
Don't resurrect threads with another question - create your own thread.
That isn't a particularly good use of macros even in C.
You can run java code through a preprocessor which most C/C++ compilers have.
You can also use code gen to achieve the similar result.