| Sign In/My Account | View Cart |
O'Reilly Book Excerpts: Java 1.5 Tiger: A Developer's Notebook
|
Related Reading
Java 5.0 Tiger: A Developer's Notebook |
Editor's note: O'Reilly's new Developer's Notebook series focuses on learning by doing -- you get the goods straight from the masters, in an informal and code-intensive style suited to software developers. Java 1.5 Tiger: A Developer's Notebook, by bestselling Java authors Brett McLaughlin and David Flanagan, follows the same task-oriented format, jumping you right into Tiger. The book covers the important new features of Java 1.5, including generics, boxing and unboxing, varargs, and much more.
In this excerpt from Chapter 5 of the book, Brett and David cover how to create and iterate over variable-length argument lists (better known as varargs), which will have you writing better, cleaner, more flexible code in no time.
We are presenting this book excerpt as a PDF download. The file size is a little over 190KB. Download Chapter 5: "varargs."
Brett McLaughlin had developed enterprise Java applications for Nextel Communications and Allegiance Telecom. When that became fairly mundane, Brett took on application servers. He then got hooked on open source software, and helped found several cool programming tools, like Jakarta turbine and JDOM.
David Flanagan is the author of a number of O'Reilly books, including Java in a Nutshell, Java Examples in a Nutshell, Java Foundation Classes in a Nutshell, JavaScript: The Definitive Guide, and JavaScript Pocket Reference.
Return to ONJava.com.
Showing messages 1 through 5 of 5.
Are varargs an improvement?Thanks for your comments. I think it's true that varargs have been pretty heralded in Tiger. That said, I've always maintained (or tried to maintain) that they are more about convenience than functionality.
I think what you'll find is that many Java programmers, especially ones for which Java is their first language, really aren't as comfortable as you might think with arrays. It's simply easier to works with Java collections. For these reasons, the idea of converting arguments to an array is a bit of a pain.
And, I do think there are valid uses of varargs, like the classic max(int...) type of method. It really is a pain to throw ten or fifteen variables into an array just to call a method like this (max int[]), at least in my opinion.
All that said, you're very much right that varargs can introduce a lot of hinky bugs. Like almost any new feature, it will probably be overused until some well-defined usage patterns develop. But, that's to be expected with new APIs :-)
Thanks for your comments!
-Brett McLaughlin
I've never had a problem with supplying multiple arguments to a method in Java... I just use an array (hmmm... sort of like Java 1.5 does). So all varargs does is replace the [] with a NEW, SEMANTICALLY EQUIVALENT symbol, the ..., and save some typing for the method's clients:
foo(one, two, three)
instead of:
foo(new Object [] {one,two,three)
Worse yet, you have to remember to typecast arrays passed to vararg methods to avoid them being treated as the argument list... unless that's what you want.
Further, as opposed to explicitly using arrays, varargs can be used only once in the parameter list, at the end. (Also, I think the author alluded to another problem in the excerpt:
void foo(String x, String... list) {}
void foo(String x, String y, String... list) {}
foo("a","b","c");
where it is unclear which method to match and the compiler will return an awkward error. Using arrays would avoid this ambiguity.)
Also, using varargs could degrade coding style... and lead to mistakes which would have been caught as type errors using arrays.
Ex:
void sendMail( Person sender, Person... recievers);
sendMail( myself, reciever1, receiver2 );
sendMail( recevier1, recevier2 );
Both of those method invocation will compile and work, but one of them won't do the right thing (because we forgot the "sender" argument). The mistake would have been caught by the type system if the method had been defined as:
void sendMail( Person sender, Person [] receviers );
sendMail( myself, new Person [] {receiver1, receiver2} );
Finally, I want to say that I liked the style of the book, and am considering buying it... however, the editors might like to fix the manuscript up for the next printing, because at least in the excerpt, there are a number of places where the code doesn't seem to match the output, or where the '-' is used when a "=" should have been.
Best,
Marcin