Chapter 7. The for/in Statement

One of the coolest things about Tiger is that it offers so many new language features. When Java 1.3 and 1.4 were released, they had some new goodies, but most of the changes were either implementation issues (like all the Collection class restructuring), or things you didn’t use everyday (like proxies). Tiger is very different, though—you get to write new, funky-looking code, and that’s about as good as it gets for hardcore developers.

This chapter examines one of these new language features, the for/in loop. This name is a bit deceiving, as the loop never uses the in keyword; as a result, it’s often called enhanced for , and even sometimes foreach. No matter what you call it, though, it’s mostly a convenience function—it doesn’t make Java do anything particularly new, but it does save some keystrokes. If you’re an emacs or vi guy, that’s pretty nice—the less you type, the more advanced a programmer you must be, right?

Ditching Iterators

At its most basic, the for/in statement gets rid of the need to use the java.util.Iterator class. That class, useful in looping over collections of objects, was rarely useful in and of itself. Instead, it made looping, and accessing the objects in that loop, possible. As ...

Get Java 5.0 Tiger: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.