my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variable length method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Variable length method " Watch "Variable length method " New topic
Author

Variable length method

Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
public class Prg{
public static void main(String[]args){
fun(null);
}
public static void fun(Integer...a){}
}

Why this program generate warning message?

But when i change fun(null,null) then this program does not generate any warning message.
[ November 03, 2007: Message edited by: Prasun Howlader ]

"Control time instead of letting time control you."
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Because "fun(null)" is ambiguous when varargs are involved. Do you want to pass an array with a single null element, or do you want the array reference itself to be null? To remove the ambiguity, you should use either
for an array with a single null element, or
for an array reference that is null.
For backward compatibility, the compiler will default to the second behavior when it encounters these ambiguous statements.


SCJP 5.0
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi !!!
Your programm is generating the warning because null is not an Integer.
If you don't want the warning you should cast null as an Integer:



SCJP 5.0 | SCWCD 1.4
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Collins Mbianda:

Your programm is generating the warning because null is not an Integer.
If you don't want the warning you should cast null as an Integer:

That's not quite right. null is a valid value for an Integer reference argument. For example, this program will not generate any cast warnings:

As explained above, the actual source of the warning is an ambiguity between Integer and Integer[] as the null reference type.
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi Kelvin !!!
Thanks for correcting my mistake.
I'm agreing with you: it's a problem of ambiguity.
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
But when i change fun(null,null) this program produces no warnings why?
[ November 03, 2007: Message edited by: Prasun Howlader ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Prasun Howlader:
But when i change fun(null,null) this program produces no warnings why?

Because there is no ambiguity in that. This definitely means that you want to pass a two-element array containing null and null. It's not possible to interpret this as passing a null array reference, since you specified two parameters and the method only accepts one varargs parameter (remember, varargs in Java are implemented using arrays).
[ November 04, 2007: Message edited by: Kelvin Lim ]
Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
Thanks Kelvin Lim now i understand this.
 
GeeCON Prague 2014
 
subject: Variable length method