Win a copy of Fixing your Scrum this week in the Agile 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

var-args and overloading

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one tell me why the following code does not compile ?

 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because int is applicable for both - int and double.

If you try by passing a double value, it will work fine
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because int can be widened to a double, so it gives ambigous method.
If you change the double to a short, it will compile
Or else instead of passing int you can pass a float or double.

 
Amitav Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following the same logic below code should not compile:

But it compiles fine giving the ouput "Integer". Any idea why the behaviors are different for var-args and auto boxing?
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amitav Chowdhury wrote:Following the same logic below code should not compile:

public class Overloading {

static void overload(Integer x){
System.out.println("Integer");
}

static void overload(Double d){
System.out.println("Double");
}

public static void main(String args[]) {
int i=1;
overload(1);
}
}

But it compiles fine giving the ouput "Integer". Any idea why the behaviors are different for var-args and auto boxing?



I suggest looking at this link here. It explains some basic rules of widening, boxing and var-args.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrappers are peers to each other. They cannot be converted from one type to other.
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rushikesh sawant wrote:Wrappers are peers to each other. They cannot be converted from one type to other.


very correct!
All wrapper classes inherit from abstract class "Number" except for Boolean and Character.
A Double is a Number
An Integer is a Number
But Double is NOT an Integer


hope this helps!
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic