Skip to main content

Intro to Variable Arguments

Posted by daniel on April 19, 2004 at 8:00 AM PDT

What do you do if your method requires a variable number of variables?

Jeff Friesen has launched a new column called Java Tech with an article on Using Variable Arguments. Jeff continues to write a Java 101 column for JavaWorld magazine and has started his column for us with a look at some of Tiger's new features. What if you want to add a collection of numbers but you don't know how many will be in your collection? This is Jeff's first example.

static int sum (int ... numbers)
   int total = 0;
   for (int i = 0; i < numbers.length; i++)
        total += numbers [i];
   return total;

Now you can call this method as sum(10,20) or sum(18,20,305,4). There are certainly other ways to handle this particular case, but it is a nice illustration to draw you into the article. What are your thoughts on var args?

In today's Weblogs , Eduardo Pelegri-Llopart blogs about the Total Cost of Development and Developer Communities. He includes Training, Examples, Support, and Risk of choosing the right solution as contributors to TCD. He says that these costs "are very well addressed by developer communities. The example that I often use is Struts . I don't want to minimize the technical benefits of Struts, but I believe that the main reason why Struts is so succesful is the very strong developer community built around the code which help reduce the tocal cost of development by addressing the contributors I mention above."

Simon Brown has started a discussion on running "one-time set up and tear down logic" in his test classes in Global setUp() and tearDown() in JUnit tests. He uses a static initializer block with a shutdown hook for set up and tear down logic.

Although he is writing about JXTA in the specific, Mike Duigou's entry Evolution is an interesting look at what happens as you discover problems with an API. In his example he talks about the problems with adding a new public method to an API and references Fred Brooks' point that if you do a rewrite of your code "The users also won't be thrilled to have to rewrite their code to use the 2.0 release, there's the risk that the user base will fork between the 1.0 and 2.0 release. The user base of the product is growing though and new users will use only the new APIs."

Also in Java Today
, Art Gould interviews Satya Kolachina on Linux Enterprise Application Development. Kolachina says that "The Java platform mitigates risks that I see with open source because it separates applicatin development from the underlying layer. There aren’t many Linux vendors providing tools and SDKs; they primarily provide distributions. The inventors of Java have really helped the Linux platform: there are a lot of IDEs and other tools available. So I believe that the survival and health of Linux is enhanced by Java platform."

Jason Briggs is helping those of you Migrating to Velocity. If you already have a JSP-based project, there is some pain in moving over to take advantage of Velocity. Jason helps you make the transition and provides the motivation for doing so, saying "Velocity is a simple, yet powerful, templating engine that can be used to generate dynamic content as an alternative to the incumbent standard. In some cases it provides a performance advantage over JSPs, and it certainly forces a web developer to think differently about separating content from code."

In today's Projects and Communities , the Java Desktop community's Swing Sighting preview on their home page features some screenshots of Memoranda, an open-source personal project and diary tool.

Take a tour of Sun Java Studio Enterprise in a free webinar on April 21st, 9am PDT. You will need to register for this Sun Developer Network event.

In today's News Headlines

Registered users can submit news items for the href=""> News Page using
our news submission
. All submissions go through an editorial review by news director
Steve Mallet before being posted to the site. You can also subscribe to
News RSS feed

Current and upcoming
Java Events

Registered users can submit event listings for the href=""> Events Page using our href=""> events submission
form. All submissions go through an editorial review before being
posted to the site.

Archives and Subscriptions: This blog is delivered weekdays as the
Today RSS feed
. All members can subscribe to the email
updates for the site at the href="">
java-net Mailing Lists page. You must be logged in to subscribe
the javanet_Daily and javanet_Weekly lists. Also, once this page
is no longer featured as the front page of
it will be archived along with other past issues in the href=""> Archive.