Codessentials

  • Increase font size
  • Default font size
  • Decrease font size
Home Coding tips Java How to use VarArgs

How to use VarArgs

The following example explains how to use varargs.
Import to know is that the varargs paramers must come last in the parameter list.
public class VarArgsExample {
String concat(String... words)
{
String sentence = "";
for(int i=0; i<words.length; i++)
{
sentence += words[i] +" ";
}
return sentence.trim();
}

public static void main(String args[])
{
VarArgsExample va = new VarArgsExample();
String aSentence = null;

aSentence = va.concat("hello","world","!");
System.out.println(aSentence);
}
}

This example would print:

hello world !
 

Bookmark

AddThis Social Bookmark Button


Newsflash

Now we have got some coding tips for the developers amongst you!