This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes var-args and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "var-args and overloading" Watch "var-args and overloading" New topic
Author

var-args and overloading

Amitav Chowdhury
Greenhorn

Joined: Nov 13, 2009
Posts: 10
Can some one tell me why the following code does not compile ?

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Because int is applicable for both - int and double.

If you try by passing a double value, it will work fine
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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

Joined: Nov 13, 2009
Posts: 10
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?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
Wrappers are peers to each other. They cannot be converted from one type to other.


SCJP 5.0 100%
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: var-args and overloading
 
Similar Threads
Confused about overloading
who can tell what is the method parseInt( )?
I can't understand these code!
BETA: Method resolving, un/boxing varargs mock question
reference to method1 is ambiguous ..... java can't decide whic one to call.