Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable length method

 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kelvin !!!
Thanks for correcting my mistake.
I'm agreing with you: it's a problem of ambiguity.
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kelvin Lim now i understand this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic