posted 14 years ago
...
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