• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code explanation needed

 
Mohd Abrar Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Weiheng Zhang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...
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
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
swapna mallipudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

and thanks others for helping me out.
 
Weiheng Zhang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic