• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic