VarArgs for String in Java

The following example illustrates Varargs for String using a simple static method in for easy understand

class VarArgsExample
{
public static void main(String args[])
{
concat("java","-","demos",".","blogspot",".","com");
concat("Gowtham ","Gutha");
}

public static void concat(String... s)
{
String temp="";
for(int i=0;i<s.length;i++)
{
temp+=s[i];
}
System.out.println(temp);
}
}

Output


java-demos.blogspot.com
Gowtham Gutha

Explanation

Not only for primitive types like int varargs in Java is applicable for wrapper classes like String also. I'd just like to say that not just for wrapper classes, again varargs is applicable for any class. The logic is some thing used for concating more than 2 strings. This is the way of using varargs for String class, in a similar way for other classes too.
Do not forget to see varargs for integer (primitive) type also.

No comments: