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 code explanation needed 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 "code explanation needed" Watch "code explanation needed" New topic
Author

code explanation needed

Mohd Abrar Khan
Greenhorn

Joined: Jan 20, 2010
Posts: 18
Hi i am unable to arrive at the correct answer for the below code - Chap3 Q9 (K&B)


The problem is i am not getting the actual Auto Boxing and wideining logic, could any one explain me these two concepts in simple form.

Thanks!

Abrar
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

may be this can help you...
Remember the rules: widening is preferred always over Auto Boxing, and Boxing is preferred over var-args always.

for example

void test(Integer i) and void test(long l)
int i = 10;
invoke => test(i)
will call test(long). //widening is preferred over boxing.

similar

void test(Integer i, Integer i2) and test(int... i);
int i = 10;
invoke => test(i, i);
will call test(Integer, Integer) //boxing preferred over var-args.

Hope this helps.
the best way to clear such doubts write more and more code yourself...


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Weiheng Zhang
Greenhorn

Joined: Jan 23, 2010
Posts: 9
...
Principles in Chapter 3:
- Widening beats boxing (primitive needs less resources and is processed fast than object)
- Widening beats var-args
- Boxing beats var-args

My explanation:

Line 7. doStuff(x, y);
x is an int, can be autoboxed to Integer (that's Boxing, primitive type is handled like objects), then an object of Integer can be widened to an Object (that's widen, Integer is-a subclass of Object)
y is a Boolean, can be widened to an Object
x and y both match type of Object, so Line 13 executes. s += "2";

Line 8. doStuff(x);
x can be boxed to an Integer or Object, but it can't be a Long, for Integer and Long are different subclasses of Number, casting fails.
and var-args is picked up last, 14. doStuff(Integer... i) has lower priority than 12. doStuff(Object o).
so Line 12 executes. s += "1";

Line 9. doStuff(sa, sa);
despite the elements are of short type, sa itself is an object, (sa, sa) applies to doStuff(Object... o)
Line 13 executes. s += "2";

Result: 212


- SCJP SCWCD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Mohd 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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Line 8. doStuff(x);
x can be boxed to an Integer or Object, but it can't be a Long, for Integer and Long are different subclasses of Number, casting fails.
and var-args is picked up last, 14. doStuff(Integer... i) has lower priority than 12. doStuff(Object o).
so Line 12 executes. s += "1";


why doStuff(Object o) not doStuff(Integer.... i)? I thought Boxing and Varags are allowed, then it should be doStuff(Integer.... i) right? And what do you mean by doStuff(Integer... i) has lower priority than doStuff(Object o)?

Weiheng Zhang, your explanation for each question is so clear and understandable. Congrats on your SCJP score.
Mohd Abrar Khan
Greenhorn

Joined: Jan 20, 2010
Posts: 18

Thanks Weiheng Zhang for your explanation, it was very clear and simple..

and thanks others for helping me out.
Weiheng Zhang
Greenhorn

Joined: Jan 23, 2010
Posts: 9
swapna mallipudi wrote:Line 8. doStuff(x);
x can be boxed to an Integer or Object, but it can't be a Long, for Integer and Long are different subclasses of Number, casting fails.
and var-args is picked up last, 14. doStuff(Integer... i) has lower priority than 12. doStuff(Object o).
so Line 12 executes. s += "1";


why doStuff(Object o) not doStuff(Integer.... i)? I thought Boxing and Varags are allowed, then it should be doStuff(Integer.... i) right? And what do you mean by doStuff(Integer... i) has lower priority than doStuff(Object o)?

Weiheng Zhang, your explanation for each question is so clear and understandable. Congrats on your SCJP score.


Hi Swapna,
You're right, boxing and var-args are both allowed, but as per Java mechanism for choosing available overloaded methods, var-args comes last. If there is not doStuff(Object o), it will pick up doStuff(Integer... i).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code explanation needed
 
Similar Threads
Advanced overloading
Method overloading
Question about boxing and args
Advanced Overloading (widening,boxing,var args)
Wrapper