a newbie's question varargs(....)

M

Mike

Hi

I've read java doc.
I cannot understand what varargs(....) is .
Can someone give me a very very simple example?

thanks.

Mike
 
D

Daniel Pitts

Hi

I've read java doc.
I cannot understand what varargs(....) is .
Can someone give me a very very simple example?

thanks.

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

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

Mike

I've read java doc.
I cannot understand what varargs(....) is .
Can someone give me a very very simple example?

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

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.


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
 
J

Jussi Piitulainen

Mike writes:
....
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.
 
F

frustratedprogrammer

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?
 
D

Daniel Pitts

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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top