aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes varargs and boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "varargs and boxing" Watch "varargs and boxing" New topic
Author

varargs and boxing

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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 ???


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Nik
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Chandra
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks dhwani

The link was wonderful
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: varargs and boxing
 
Similar Threads
Doubt on Method Overloading using Var-args
widening,boxing and var args??
Var args ... explanation needed
Doubt with compiler error about method matching
Overloading in Combination with Var-args