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