gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
KVM over IP
Disney World Tickets
Dental Insurance
Televisions
Compare Prices
Promotional Golf
Find Software
Home Improvement
Build a Server Rack
Data Center Solutions
KVM Switches
Imprinted Promotions
GPS Devices
Online Shopping

 
Biz Resources
Ecommerce Hosting
Dedicated Server Hosting
Data Recovery Services


Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Looking at Varargs in J2SE 1.5
By Jeff Langr

Java 1.5 is slated to be released by Sun in late summer 2004. It contains many significant new language features and will dramatically alter the look and feel of Java code. The new varargs language feature was introduced to allow more flexibility and simplicity in coding.

Varargs

The varargs, or variable arguments, feature allows a developer to declare that a method can take a variable number of parameters for a given argument. The vararg must be the last argument in the formal argument list.

You might use varargs to allow adding one to many objects to a collection that you have defined by using a single add method call. Suppose you need a method that writes any number of records to the console. Using varargs, you can make any of the following method calls:

write("line 1");
write("line 2", "line 3");
write("line 4", "line 5", "line 6", "line 7", "line 8", "line 9",
      "line 10");

Before 1.5, you would need to define overloaded versions of the write method to accomplish this. This isn't an effective solution if you want to be able to support any number of records being passed in.

The write method introduces a couple new bits of syntax:

public void write(String... records) {
   for (String record: records)
      System.out.println(record);
}

First, the records argument is defined as type String.... This indicates to the compiler that calling code can pass a variable number of String parameters. For all other intents and purposes, however, String... equates to a String array (String[]).

The Enhanced For Loop

Second, the code uses the new enhanced for loop syntax that is being introduced in Java 1.5. You read the for loop in the example above as "for each String record in records." Or more explicitly, "for each object stored in the String array records, assign the object to a reference named record of type String."

The enhanced for loop, also known as foreach, is a nice feature that can also be used with any of the collection classes in Java 1.5. In addition, Java supplies hooks that allow you to make your own class iterable, so that it can be referenced in a foreach loop.

Behind the Scenes

There isn't any real magic going on with respect to varargs. Behind the scenes, Java is converting the new syntax into equivalent code that would work under Java 1.4 and older versions. The example above is equivalent to coding the following:

write(new String[] { "line 1" });
write(new String[] { "line 2", "line 3" });
write(new String[] { "line 4", "line 5", "line 6"});

// ... 

public void write(String[] records) {
   for (String record: records)
      write(record);
}

One could view varargs as pure syntactic sugar. It does help remove some of the cruft from your code.

Java 1.5 includes a Formatter class that depends upon the varargs feature to be effective. This new class allows you to use C-like format strings so that you can easily produce formatted output. I'll discuss this class in a follow-up article.

Trying it Out

You can try out the above code by downloading the current beta version of J2SE 1.5 from Sun's Web site. The download page is located at http://java.sun.com/j2se/1.5.0/download.jsp.

About the Author

Jeff Langr is a freelance author and the owner of Langr Software Solutions. He is working on a second book on Java and test-driven development entitled Agile Java, due out from Prentice Hall in fall 2004.


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives

Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Five Trends for Application Development & Program Management. Download Complimentary Report Now.
Whitepaper: Embeddable Content Platform for OEM's



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES