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("Santa", "Bye");

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

public static void hello(String... names) {
final String defaultName = "Duke";
String msg = "Hello, ";
msg += ((names.length == 0) ? defaultName : StringUtils.join(names, ", "));
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)


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);


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

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

The name of the remote procedure.

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

Specifies all the parameters, save the final, varying, parameter.

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.

Unknown 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.



Unknown 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

Unknown 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

richard bryan said...

great java tips At SynergisticIT we offer the best java bootcamp