• 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

Wrapper

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: K&B Book.

public class Wrapper
{
static String s="";
public static void main(String[] args)
{
int x=4;
Boolean y=true;
short[] sa={1,2,3};
dos(x,y);
dos(x);
dos(sa,sa);
System.out.println(s);
}


static void dos(Object o)
{
s+="1";
}
static void dos(Object... o)
{
s+="2";
}
static void dos(Integer... i)
{
s+="3";
}
static void dos(Long L)
{
s+="4";
}
}

Here,dos(sa,sa) invokes the method static void dos(Object... o), but shouldnt the method void dos(Integer.. i) be invoked instead??
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi

invocation of dos(Object... o) happens because BOXING + WIDENING happens here:
Boxing -- Primitive to Wrapper -- short to Short
Widening -- Lower to Higher -- Short to Object
This is allowed.

Whereas,
If it were dos(Integer.. i) the following would have been the scenario which is NOT allowed:
short to Short
Short to Integer -- which is not allowed, because Short and Integer are siblings.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic