> Hi
>
[quoted text clipped - 5 lines]
>
> Mike
class VeryVerySimpleExample {
public static void printArgs(String...args) {
String[] array = args;
System.out.println(array.length + " " + args.length);
for (int i = 0; i < args.length; ++i) {
System.out.println(args[i]);
}
}
public static void main(String[]args) {
printArgs();
printArgs("Hello");
printArgs("Hello", "World");
printArgs(args);
printArgs(new String[] {"This", "is", "not", "a", "drill"});
}
}
So, hopefully my example shows you that:
A vararg is simple syntactic sugar for an array.
The Java compiler will automatically create a new array if one isn't
passed in.
There are a few other details to know about, but thats the basis of
it.
Hope this helps,
Daniel.
Mike - 17 Apr 2007 08:58 GMT
> > Hi
>
[quoted text clipped - 34 lines]
> Hope this helps,
> Daniel.
thank you very much. Daniel.
Though it's a little difficult for me.
It's quite strange there's no space between String... and args.
public static void printArgs(String...args) {
> String[] array = args;
"array" is also an array with variable length. right?
I tried to replace all the String to int, which is more familiar to
me.
I also do a little test. I understand now.
thank you.
Mike
Jussi Piitulainen - 17 Apr 2007 09:07 GMT
...
> It's quite strange there's no space between String... and args.
> public static void printArgs(String...args) {
No stranger than the space that is not between String[] and args:
public static void printArgs(String[]args) {
You can put it in when you write your own code. I do:
public static void printArgs(String [] args) {
public static void printArgs(String ... args) {
Matter of taste, whether shared by many or few. Compiler cares not.
frustratedprogrammer@gmail.com - 18 Apr 2007 01:01 GMT
> You can put it in when you write your own code. I do:
>
> public static void printArgs(String [] args) {
> public static void printArgs(String ... args) {
>
> Matter of taste, whether shared by many or few. Compiler cares not.
Are you saying there is absolutely no difference between
public static void printArgs(String [] args) {
public static void printArgs(String ... args) {
If so why did they bother to introduce the new syntax for the second
in Java 5?
Daniel Pitts - 18 Apr 2007 02:15 GMT
On Apr 17, 5:01 pm, frustratedprogram...@gmail.com wrote:
> > You can put it in when you write your own code. I do:
>
[quoted text clipped - 10 lines]
> If so why did they bother to introduce the new syntax for the second
> in Java 5?
The only real difference is when passing values into it.
public void printArgs(String[]args); can only be passed an array
printArgs(new String[] {"a", "b", "c"});
But, void printArgs(String...args); can be passed an array, or several
values.
printArgs("a", "b", "c");
The feature allows one to make intent clear without excess syntax.