12/17/2008

Pass optional params with varargs

Varargs (introduced in Java SE 5) allows you to pass 0, 1, or more params to a method's vararg param. This reduces the need for overloading methods that do the similar things. For example,

package javahowto;

import org.apache.commons.lang.StringUtils;

public class HelloWorld {
public static void main(String[] args) {
hello();
hello("World");
hello("Santa", "Bye");

final String[] names = {"John", "Joe"};
hello(names);
}

public static void hello(String... names) {
final String defaultName = "Duke";
String msg = "Hello, ";
msg += ((names.length == 0) ? defaultName : StringUtils.join(names, ", "));
System.out.println(msg);
}
}
org.apache.commons.lang.StringUtils is in commons-lang-2.4.jar. Running this example gives the following output:
Hello, Duke
Hello, World
Hello, Santa, Bye
Hello, John, Joe
BUILD SUCCESSFUL (total time: 0 seconds)

6 comments:

zubehör said...

The [vararg] attribute specifies that the function takes a variable number of parameters. To accomplish this, the last parameter must be a safe array of VARIANT type that contains all the remaining parameters :
[vararg [, optional-attributes]] return-type function-name(
[optional-param-attributes] param-list,
SAFEARRAY(VARIANT) last-param-name);

Parameters
optional-attributes

Specifies zero or more attributes to be applied to the function. Separate multiple attributes with commas.
return-type

The type of the data returned by the remote procedure upon completion.
function-name

The name of the remote procedure.
optional-param-attributes

Specifies zero or more attributes to be applied to the function parameter immediately following the attribute listing.
param-list

Specifies all the parameters, save the final, varying, parameter.
last-param-name

The name of the varying parameter.

Return Value :
This attribute does not return a value.

JP @ code review checklist said...

Varargs is indeed a very interesting feature but still under utilized. you know even main method is written using variable argument. by the way I also blogged my experience as variable arguments in java let me know how do you find it.

Java Tuts said...

I would like to add that vargs are implemented as a array behind scenes. But there are many cases where the compiler can get confused about the variables arguments being passed and unable to resolve the passed arguments to the acceptable arguments for a method.

Silvia Jacinto said...

Time is gold. We should treasure every second of it so that in time we will not regret anything because we only live ones.I have read your article and i was glad and inspired. Thanks for it, Cheers!
Visit my site if you have time, Enjoy.

n8fan.net

www.n8fan.net

Steve Smith said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000