| Article: |
Creating Varargs in Java 1.5 Tiger | |
| Subject: | Are varargs an improvement? | |
| Date: | 2004-08-14 20:30:22 | |
| From: | MarcinJeske | |
|
I have read a number of articles and comments lauding varargs as one of the best new features in 1.5. I don't get it. They just seem to be a slight change of syntax and a change to coding style. Further, while they save some people a bit of typing, they introduce potentially elusive bugs (as noted in the excerpt).
|
||
Showing messages 1 through 4 of 4.
-
Are varargs an improvement?
2005-01-11 01:34:11 Krishnas [View]
-
Are varargs an improvement?
2006-02-04 19:02:20 bastos [View]
Oh yes. I agree.
Java was quite unattractive before version 1.5, in my opinion, the aggrivating amount of typecasting involved with any type of general consumption libraries (Such as the Collections framework) was not only an eyesore, but aggravated many of our repetitive strain injuries. Personally, I stayed as far away from java as possible due to the early large amount of work for low gain and low efficency of java. A decade later, they have solved what originally made me sick about it. With the combination of generics, for-in loops, enums, static imports, and regular string formatting (as well as its catalyst, variable argument methods), all of a sudden I don't mind coding java anymore.
And with the great runtime efficiency boost in the latest JVM, I don't writing, running, and using java applications, either...so I've crossed over, quit my job, and now work as a Java Engineer. And to think 10 years ago when I first heard "Java is the future", and grabbed a java compiler and started hacking...I found myself laughing in disbelief.
While its true that variable arguments increase the possibility of runtime error, eliminating the possibility of runtime error entirely causes entirely more trouble for the engineers using the tools than its worth. And not having features like these don't stop people from writing buggy, screwed up code, anyway.
-
Are varargs an improvement?
2004-08-19 06:36:52 Brett McLaughlin |
[View]
Marcin-
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
-
Are varargs an improvement?
2004-10-08 15:55:03 MarcinJeske [View]
It's good to see that you are keeping an eye on comments. Just to add a few thought:
You may be right that first-time programmers learning Java, as opposed to those coming from other languages, are not as confortable using arrays. After working with arrays in C/C++, using them in Java is a dream... the same simplicity and power but without the inherent bugs (pointer error, bounds-checking, type errors). As much as the collections classes are necessary and useful to describe dynamically changing groups, set-style notation, and underlying performance needs, nothing beats the ease of creating, initializing, and altering elements in an array. ( new int [] {1,2,3} )
On the other hand, I wouldn't mind if there were improvements. After becoming familiar with the array operations of Python and Perl, I've found myself wishing for those operations Java, and creating helper methods which mimic them:
Namely, array slices, where a new array is returned containing a subset of the original between a range of indices ( public Object [] slice(Object [], int start, int end); ), or a subset is returned based on an array of indices ( public Object [] slice(Object [], int [] indices); )
I much prefer the Python to the Perl syntax...
Anyway, I appreciate the response.
Marcin



If you are seeing the Java 1.5 new features, ut will reduce the work for programmers rather than focusing on more functionality.
But generics are realy amazing feature indeed.using enumerationa and varargs and static imports are minmize the code.
Correct me if i am wrong.
Thank you.