question about method signature void methodCall(Object... obj) { ... }

for a method call:

publicvoid methodCall(Object... obj){

...

}

This would allow zero to many arguments, and obj is as an array.

1) does it equivalent to this?:

publicvoid methodCall(Object[] obj){

...

}

2) is there something that's zero to one? which means, either no argument, or only one argument pass in.

Regards

Eddie

[635 byte] By [eddiecjchianga] at [2007-11-27 11:05:13]
# 1

> for a method call:

>

> > public void methodCall(Object... obj) {

>...

> /code]

> This would allow zero to many arguments, and obj is

> as an array.

>

> 1) does it equivalent to this?:

> [code]

> public void methodCall(Object[] obj) {

>...

>

Pretty much. You don't have to pass an array, the varargs (as the ... is known) can accept an arbitrary number of arguments of the same type, between 0 and whatever the upper bounds on method arguments will allow

> 2) is there something that's zero to one? which

> means, either no argument, or only one argument pass

> in.

>

No. You can mitigate this by overloading the method

public void doSomething(RandomObject obj, SomeOtherObject oher) {

// do stuff

}

public void doSomething(RandomObject obj) {

doSomething(obj, null);

}

georgemca at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...
# 2

Or:

public void methodCall(Object obj) {

if (obj == null) {

obj = ...defalt object value... //perhaps

}

...

}

Hippolytea at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...
# 3

> Or:

> [code]

> public void methodCall(Object obj) {

>if (obj == null) {

>obj = ...defalt object value... //perhaps

> }

>...

> /code]

Which requires that we provide "null" when we mean "I don't want to bother with that argument". Brrrrr

georgemca at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...
# 4

> Which requires that we provide "null" when we mean "I don't want to bother with that argument". Brrrrr

Yes, it's a YMMV moment...

Hippolytea at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...
# 5

I see, so that,

void methoCall(Object... obj) {

...

}

has a difference to

Object[] obj

where the caller doesn't have to specify any parameter, and the latter would require at least something or null.

I wonder if later Java can support zero to one using some notation like Object.

thank you all very much.

eddiecjchianga at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...
# 6

> I see, so that,

> > void methoCall(Object... obj) {

> ...

>

>

> has a difference to

> Object[] obj

> where the caller doesn't have to specify any

> parameter, and the latter would require at least

> something or null.

Why, aye man

> I wonder if later Java can support zero to one using

> some notation like Object.

>

Doubtful. The reason they finally succumbed and put varargs in was to allow for C-style print formatting (printf), not just because it's handy :-)

georgemca at 2007-7-29 13:07:15 > top of Java-index,Java Essentials,New To Java...