• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Method overloading

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is from SCJP guide from seira and berts
Please explain me the output of the following code:

public class Bertha{
static String s="";
public static void main(String args[])
{
int x=4;
Boolean y=true;
short [] sa={1,2,3};
doStuff(x,y);
doStuff(x);
doStuff(sa,sa);
System.out.println(s);
}
static void doStuff(Object o)
{
s+="1";
}
static void doStuff(Object... o)
{
s+="2";
}
static void doStuff(Integer...i)
{
s+="3";
}
static void doStuff(Long l)
{
s+="4";
}

The output of the code is 212
When the doStuff method is called second time,an integer variable is passed
to the method,but still the method with Object argument is called instead of method
with var-args of Integer?Can anyone explain this behaviour?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because var args are chosen the last that is if no other options is available then only var args version will be chosen.

widening is prefered over boxing.
boxing is prefered over var args.

so int will be boxed and then widened to Object rather than boxing and chosing var args.

this might help you.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anurag please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
 
Anurag Malaviya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neha Daga wrote:because var args are chosen the last that is if no other options is available then only var args version will be chosen.

widening is prefered over boxing.
boxing is prefered over var args.

so int will be boxed and then widened to Object rather than boxing and chosing var args.

this might help you.


Thanks for helping me out.........
I am also looking for working on a project.
But i am not able to decide,whether i should work on web based project or a project that involves networking
please suggest
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic