Apparently while I was away, or should I say coding with one hand tied behind my back, varargs were added to Java 1.5. It’s about time.

Pre 1.5 you had to do this monstrosity:

Object[] arguments = {
    new Integer(7),
    new Date(),
    "a disturbance in the Force"
};

String result = MessageFormat.format(
    "At {1,time} on {1,date}, there was {2} on planet "
     + "{0,number,integer}.", arguments);

Now if you simply add 3 dots (…) to the method declaration you can get true varargs:

    public static String format(String pattern,
                                Object... arguments);

which results in a much simpler usage:

String result = MessageFormat.format(
    "At {1,time} on {1,date}, there was {2} on planet "
    + "{0,number,integer}.",
    7, new Date(), "a disturbance in the Force");

Why did it take Sun so long to add such a useful feature? It only took 5 revisions of the language
to get us this. Maybe with the recent GPLing of Java, we’ll get features faster.

3 thoughts on “varargs in java 1.5

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s