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 Advanced Overloading (widening,boxing,var args) 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 "Advanced Overloading (widening,boxing,var args)" Watch "Advanced Overloading (widening,boxing,var args)" New topic
Author

Advanced Overloading (widening,boxing,var args)

David Samer
Ranch Hand

Joined: Feb 08, 2012
Posts: 49

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
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Feb 08, 2012
Posts: 49

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
 
Don't get me started about those stupid light bulbs.
 
subject: Advanced Overloading (widening,boxing,var args)
 
Similar Threads
Method overloading
code explanation needed
Question about boxing and args
Wrapper
Var args ... explanation needed