Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

varargs and boxing

 
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nik
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra
 
Guru dhaasan
Ranch Hand
Posts: 126
VI Editor Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks dhwani

The link was wonderful
 
please buy this thing and then I get a fat cut of the action:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic