13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

70Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to JavaAs in the if and while statements, the braces are opti<strong>on</strong>al when <strong>on</strong>ly asingle statement is involved, but good practice compels us always to use thebraces. Additi<strong>on</strong>al code can easily be added without messing up the logic—should <strong>on</strong>e forget, at that point, the need to add braces.Speaking of the while loop: When do you use a for and when do youuse a while loop? The big advantage of the for loop is its readability. It c<strong>on</strong>solidatesthe loop c<strong>on</strong>trol logic into a single place—within the parentheses.Any<strong>on</strong>e reading your code can see at <strong>on</strong>ce what variable(s) are being used toc<strong>on</strong>trol how many times the loop executes and what needs to be d<strong>on</strong>e <strong>on</strong> eachiterati<strong>on</strong> (e.g., just increment i). If no initializati<strong>on</strong> is needed before startingthe loop, or if the increment happens indirectly as part of what goes <strong>on</strong> in thebody of the loop, then you might as well use a while loop. But when the initializati<strong>on</strong>and iterati<strong>on</strong> parts can be clearly spelled out, use the for loop forthe sake of the next programmer who might be reading your code.The for loop with iterators. As of Java 5.0, there is additi<strong>on</strong>al syntax for afor loop. It is meant to provide a useful shorthand when looping over themembers of an iterator. 4 So what’s an iterator? Well, it has to do with collecti<strong>on</strong>s.Uh, oh, we’re surrounded by undefined terms. One step at a time, here.Java has a whole bunch (we w<strong>on</strong>’t say “collecti<strong>on</strong>,” it’s a loaded term) of utilityclasses that come with it. We menti<strong>on</strong>ed these classes in our discussi<strong>on</strong> ofJavadoc. While not part of the language syntax, some of these classes are souseful that you will see them throughout many, if not most, Java programs.Collecti<strong>on</strong> is a generic term (in fact, it’s a Java interface) for several classesthat allow you to group similar objects together. It covers such classes as Lists,LinkedLists, Hashtables, Sets, and the like. They are implementati<strong>on</strong>s ofall those things that you (should have) learned in a Data Structures course inschool. Typically you want to add (and sometimes remove) members from acollecti<strong>on</strong>, and you may also want to look something up in the collecti<strong>on</strong>. (Ifyou’re new to collecti<strong>on</strong>s, think “array,” as they are a simple and familiar typeof collecti<strong>on</strong>.) Sometimes, though, you d<strong>on</strong>’t want just <strong>on</strong>e item from the collecti<strong>on</strong>,but you want to look at all of the objects in the collecti<strong>on</strong>, <strong>on</strong>e at atime. The generic way to do that, the way that hides the specifics of what kindof collecti<strong>on</strong> you have (linked list, or array, or map) is called an iterator. 54. This feature is related to the topic of templates and generics. See Secti<strong>on</strong> 3.5.5. The earliest versi<strong>on</strong>s of Java used an object called an Enumerati<strong>on</strong>. It does much the samething as an iterator, but with somewhat clumsier method names. Iterators also allow for a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!