O'Reilly logo

Java 5.0 Tiger: A Developer's Notebook by David Flanagan, Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required