Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as off topic by thorsten müller, BЈовић, Walter, Yusubov, gnat Oct 23 '12 at 13:20

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

It's usually a good idea to not accept an answer right away. Questions with an accepted answer tend to receive less attention from the community, which means you might have received an even better answer had you waited a while before accepting one. – Michael Kjörling Oct 23 '12 at 11:55
up vote 6 down vote accepted

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

  1. There is only one instance of this field between every instance of this class
  2. 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)

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.

share|improve this answer

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.
share|improve this answer
I really don't quite agree with "a good programmer can make advanced code still short and readable in C++". Yes, it's true in a technical sense - but it is also important to keep in mind that there is a very fine line in defining own syntax elements between "readable" and "utterly gibberish to anyone but oneself". Also, unless carefully constructed, #define'd macros can be quite brittle. – Michael Kjörling Oct 23 '12 at 11:45

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++) {

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);


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.

share|improve this answer
Thanks for the edit @jozefg - Not Enough Coffee Syndrome, I guess. – Michael Kjörling Oct 23 '12 at 11:49
if(isMorning) --codingSkills; – jozefg Oct 23 '12 at 11:50
@jozefg Something like that, though it's afternoon here. – Michael Kjörling Oct 23 '12 at 11:50
Ah well same principle – jozefg Oct 23 '12 at 11:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.