• 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

Advanced Overloading (widening,boxing,var args)

 
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone , I am new to the forums so this is my first post . Im studying a little everyday in order to take the exam and pass it.

Here I come with one of the -almost endless- doubts I probably will have while reading the incredible book of Kathy and Bert.

I hope I don't become a big pain in the ass lol, forgive me if I ask too much :P

Here the question I don't understand. ( Page 283 from the book " Sun Certified Programmer for Java 6 Study Guide" - Kathy Sierra and Bert Bates. End of the chapter 3 , self test questions )





I do understand the first and third call to doStuff but...not yet the second one. (Why not B - 232 )

First call makes the boxing of int to Integer wrapper then , being object calls to first doStuff method. (And for this I believe it doesn't make the call to the doStuff method with Long wrapper , since there's no way to box the primitive int to a Long Wrapper) (Correct me if I am wrong )
The third call pass 2 array objects to method so that's clear it takes 2 objects.
But the second one, is where I am having trouble understanding it.

int primitive can't widen, so how it takes Integer wrapper instead Object type .

Thank you in advance
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second doStuff call isn't much different than the first. In both cases we have objects that can't be widened. Passing an Integer as an Object isn't widening; an instance of Integer IS-A Object. The var-args version of doStuff( Integer... i ) is not chosen because var-arg methods are the last resort, and will only be chosen if no other option is available. If the version with the single Object parameter didn't exist, then B would be correct.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, var-args is a an array of argument. For example main(String... args) is identical to main (String[] args). If you try to overload main (String...args) by main (String[] args), you will get a duplicated method compilation error.

You can pass a String array like this String [] strings = {"1","2"} to this main(String... args) method. Or, you can pass individual strings to the main("1", "2").

Back to your question:


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"; }



1. doStuff(x,y)
The compiler will choose doStuff(Object...o) because x is boxed into Integer and y is boxed into a Boolean when it is declared.
2. doStuff(x)
The compiler has two choices : doStuff(Object o) and doStuff(Integer ...i). But var-args is chosen last. Therefore, the compiler chooses doStuff(Object o) and autobox x into Integer.
3. doStuff(sa, sa)
The compiler has only one choice: doStuff(Object...0). A short[] is an object.

What happens if we change the question like this?


int x = 4; Boolean y = true; short[] sa = {1,2,3};
doStuff(sa, sa);
System.out.println(s);
}
static void doStuff(short[]...s) { s += "1"; }
static void doStuff(Object... o) { s += "2"; }
static void doStuff(Short[]... i) { s += "3"; }



The compiler will choose doStuff(short[] ...s) over doStuff(Object ...o) because it needs to widen the short[] object to Object type.
The compiler will not choose doStuff(Short[] ... i) because short[] cannot be converted into Short[].

What happens if we change the question like this?


int x = 4;
doStuff(x );
System.out.println(s);
}
static void doStuff(Long l) { s += "1"; }



The compiler will have a compilation error because the compiler cannot widen int x into long and box it into Long. For more detail, refer to the K&B, "widening and boxing" section.

What happens if we change the question like this?


int x = 4;
doStuff(x );
System.out.println(s);
}
static void doStuff(Object o) { s += "1"; }



This is an example of boxing and widening. The integer is boxed into an Integer object and widened to an object in order to pass it to doStuff.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Spring Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Dennis Deems for the idea that got me in the way of understanding and having it clear, as well as thanks to Helen Ma for the nicely explanation with examples and detailed that made me got the doubt resolved.

Help much appreciated , thank you both of you
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic