How to define a code snippet in Java [closed]

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

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật