I know about static final
equivalent to #define
, But I am not looking for it
I have used #define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
in C++ and after that I can use REP(i,10)
instead of for(i=0; i<10; i++)
in the whole program.
But is there a way to do this in Java? I want a version of #define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
in Java.
I try it with static final
but can’t figure out.
1
First of all, static final
is not the same as #define
. define
uses the C preprocessor and goes around your code making text substitutions before the compiler even sees the code. Java has no mechanism like this.
Instead static final
is really just equivalent to static const
in C++. It simply means
- There is only one instance of this field between every instance of this class
- This field won’t change ever.
So to answer your question, in java, there are no macros and thus no direct way to do what you’re asking.
If it’s really that much of a bother, you could use higher order functions to do what you’re asking. Your function would look like:
void looper(int times, SomeInterface action){
for(int i = 0; i<times; ++i)
action.act(i);
}
}
However I think you’ll find that this is overkill. Additionally this loses several important features you’d want in a for loop, such as the ability to break out of it on demand. The ability to modify the loop counter or any variables surrounding the loop.
You’d be better served to stick to iterating over collections and for situations where you actually need to use numbers, try using a Range class.
Java 8 (whenever that gets released) will have closures, making many of these things possible. But in the meantime, I’m afraid you’ll just have to type it out.
No, it’s not possible. However in your particular case it’s not needed either.
Java is intentionally designed so that you can’t redefine anything. That makes it easier for less experienced programmers to understand code and easier for others to understand the code they write, because things can’t be redefined.
The downside is that it makes the language much less flexible, so where good programmer can make advanced code still short and readable in C++, it will inevitably be verbose in Java.
In your particular case, though:
- You should almost never iterate over range of numbers. Iterate over collections. That’s true in any language. C, C++, Java, Python, whatever.
- Java has a range-based for loop:
for(ItemType i: collection)
- You can create a Range class to iterate over range of integers like:
for(int i: new Range(0, 10))
. There does not seem to be one in standard library, but you can try one of the many samples laying around the net.
1
If you are a novice with a language, you should learn the idioms of that language, not invent your own.
As pointed out by @jozefg, Java static final
functions are not equivalent to #define
macros in C/C++. They work in different ways and serve completely different purposes.
Introducing arbitrary new syntax is guaranteed to trip up any developer not familiar with your codebase. If I see something like
for(int i = 0; i < 10; i++) {
doSomethingNthTime(i);
}
then I know exactly what it means. If I see:
REP(i, 10) { doSomethingNthTime(i); }
it isn’t anywhere near as obvious. Who is this representative that you are talking about? And even if I know that by REP
you mean “repeat”, what exact range will i
cover? I will have to look at the macro declaration to know, whereas with the plain for
loop the answer is in plain sight.
Also, plain for
allows things that your REP
macro has no way to express. How about the following?
short i;
long j, k = 0;
for(i = 0, j = 16; i < 10; i++, j <<= 2) {
doSomethingNthTime(i, j, &k);
}
or
int i;
for(i = 0; i < 16; i++) { ... }
i += values[z];
for(; i < 1024; i++) { ... }
Such complex for
expressions (although obviously with a meatier loop body) are not entirely unusual in production code, and can be extremely useful in some situations.
4