Forums Register Login

varargs and boxing

+Pie Number of slices to send: Send
in the following code:

class varArg{
static void wide_varArg(Long... x){System.out.println("LONG");}
static void box_varArg(Integer... x){System.out.println("INTEGER");}

public static void main(String at[]){
int i=5;
wide_varArg(5,5);
box_varArg(i,i);
}
}


the compilation fails with the following message
varArg.java:7: wide_varArg(java.lang.Long...) in varArg cannot be applied to (int,int)
wide_varArg(5,5);
^
1 error



What is the error here ???
+Pie Number of slices to send: Send
Hi,
the compilation fails with the following message
varArg.java:7: wide_varArg(java.lang.Long...) in varArg cannot be applied to (int,int)
wide_varArg(5,5);

wide_varArg method takes Long wrapper type.When you call method like
wide_varArg(5,5) it will be boxed to Integer wrapper type and compiler will try to widen it into Long wrapper type here the error will get generated because Wrapper type of one cannot be widen to another wrapper type.


Regards
Nik
+Pie Number of slices to send: Send
well!!below shown is the link which has two topics you refer to the first topic.

1)Using the Varargs Language Feature

It consist of the same problem as explained by you as well as the explanation for it but you will have to read it completly.
So here is the link.

http://java.sun.com/developer/JDCTechTips/2005/tt0104.html

i hope it helps you.
+Pie Number of slices to send: Send
Hi,

primitive will be boxed to corresponding wrapper and then widening is done.
Integer can't widen to Long. As Nikhil said one wrapper can't widen from
one to another.

Integer can widen to Number or Object but not to any wrapper.


Thanks,
+Pie Number of slices to send: Send
Thanks Nik
+Pie Number of slices to send: Send
Thanks Chandra
+Pie Number of slices to send: Send
Thanks dhwani

The link was wonderful
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 724 times.
Similar Threads
widening,boxing and var args??
Doubt on Method Overloading using Var-args
Doubt with compiler error about method matching
Overloading in Combination with Var-args
Var args ... explanation needed
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:42:38.