Here I found a lot of confusion with regard to autoboxing, varargs and widening. Therefore, with the help of all the ranchers and moderators I propose to compile the rules in this regard which we shall call the Golden Rules. My perception is as under, on which I invite you all for an exhaustive discussion and to incorporate any modifications, if required, so that it may help all the SCJP aspirants.
Rules :
1. Primitive Widening > Boxing > Varargs.
2. Widening and Boxing (WB) not allowed.
3. Boxing and Widening (BW) allowed.
4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
5. Widening between wrapper classes not allowed
Now Let me illustrate each of them with instances :
Dear friends, I am also a learner. So if i have made a mistake somewhere, please let me know. Moderators kindly help in this regard to consolidate the rules. Thanks in anticipation
[ July 28, 2008: Message edited by: Anand Shrivastava ]
Anand Shrivastava
SCJA
Milan Sutaria
Ranch Hand
Joined: Jul 10, 2008
Posts: 118
posted
1
great work Anand .. Keep it up just want to add little to your rule#4... widening+varArgs & Boxing+varargs are individually allowed (but not allowed in overloaded version of method)
SCJP 6 83%
Working on SCWCD/OCPJWCD
Anand Shrivastava
Ranch Hand
Joined: Jul 22, 2007
Posts: 125
posted
1
Thanks Milan, I acknowledge that rule 4 should be happily worded as under:
Rule 4 - While overloading Widening + vararg and boxing + vararg are mutually exclusive of each other. Editing it accordingly.
In this is it following the series as unboxing-widening-varargs?
Thanks,
Geeta Vemula
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
yeah, it is following unboxing+widening+varargs.
aparna shinde
Greenhorn
Joined: Dec 08, 2008
Posts: 11
posted
0
these rules are really helpful !!! Good work anand
Regards,<br />aparna
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
posted
0
Here I am getting compiler error???
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
Good catch. It seems there is an ambiguity in this case:
The call doX(1) is ambiguous as well. This looks like Rule 4 needs to be expanded, and a new rule similar to 4 added. 4. While overloading, Widening (or the identity conversion) + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together. 4' While overloading, vararg and unboxing + vararg can only be used in a mutually exclusive manner i.e. not together.
[ December 20, 2008: Message edited by: Ruben Soto ] [ December 20, 2008: Message edited by: Ruben Soto ]
All code in my posts, unless a source is explicitly mentioned, is my own.
akash azal
Ranch Hand
Joined: Jan 31, 2009
Posts: 70
posted
0
can anyone clear about line no 9 in anand's post
We will keep things moving!!
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
akash azal wrote:can anyone clear about line no 9 in anand's post
Here doX(5) will call doX(long l) that is rule 1, means int----> long that is primitive widening.
for doX(Object o), int ----> Integer boxing and then
Integer---->Object that is Widening. //rule 3
So here rule 1 preferred over rule 3.
akash azal
Ranch Hand
Joined: Jan 31, 2009
Posts: 70
posted
0
how it is processed for linr no8
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
akash azal wrote:how it is processed for linr no8
line no 8 is Boxing + widening//from rule 3
preffered over
varargs //from rule 1
Var-args should be your last preference always.
You can do one thing, just remember rule 1 as
1. widening>boxing>varargs
so you can see boxing and widening comes before var-args so doX(Object) in line 8 called.
same way for line 9, widening comes before boxing, that's why widening is preferred over (widening+boxing), so doX(long) is called.
Jose Francisco Ruiz Massieu
Greenhorn
Joined: Jan 19, 2009
Posts: 3
posted
0
well, i think that rule 4 it is not completly true due if we invoke line 10 with
it works even with overloading, (run doX(long... l) by rules 1 and 2), so in this case it is perfectly legal to have both Widening + vararg and Boxing + vararg together
what do you think???
maybe:
4) Widening + varargs , Boxing + vararg together 4.1) Without overloading Is allowed
4.2) With overloading
4.2.1) Without widening Not allowed
4.2.2) With widening Is allowed
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
Jose Francisco,
The reason why the cases that you show work is because you can't box a byte to an Integer, so the method that uses the boxing + varargs is not even considered. Let me know if that makes sense.
Jose Francisco Ruiz Massieu
Greenhorn
Joined: Jan 19, 2009
Posts: 3
posted
0
yes I know, thats why it says (run doX(int... i) by rules 1 and 2), but what it's important (I think) is that, in this case both Widening + varargs , Boxing + vararg can be together, in the same class. So
While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together
doesn't apply at all, do it???, what do you think???
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
1
Jose Francisco Ruiz Massieu wrote:yes I know, thats why it says (run doX(int... i) by rules 1 and 2), but what it's important (I think) is that, in this case both Widening + varargs , Boxing + vararg can be together, in the same class. So
While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together
doesn't apply at all, do it???, what do you think???
byte---->Integer... is not boxing+varargs.
it is
byte---->int //widening then int --->Integer //boxing then Integer... //var-args
so
byte--->Integer... is widening+boxing+var-args
So above rule is right, you are misjudging it.
Natalie Ap
Ranch Hand
Joined: Jan 09, 2009
Posts: 49
posted
0
Hi all,
Can you please explain to me why the following is gives a compile time 'ambiguous method' error and which rule covers the following possibility:
Fiona
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
a.getNum(4);
here 4 is int
so for getNum(int... n)
it is only varargs.
int ---> int...
for getNum(Integer... n)
it is boxing + varargs.
int--->Integer--->varargs
Here rule no 4 applies, but we need to make rule no 4 more clear to cover this behavior also.
Jia Tan
Ranch Hand
Joined: Jan 28, 2009
Posts: 32
posted
0
doX(int... i) & doX(long... l), call doX(5) get ambiguous too
Jia Tan
Ranch Hand
Joined: Jan 28, 2009
Posts: 32
posted
0
I guess the rules is: you cannot have more than one primitive widening + var-args (WV) overloaded methods, and you cannot have var-args(V) co-exists with other primitive widening + var-args (WV) overloaded methods
the following are all resulted in ambiguous by calling doX(5):
so for getNum(int... n)
it is only varargs.
int ---> int...
for getNum(Integer... n)
it is boxing + varargs.
int--->Integer--->varargs
Here rule no 4 applies, but we need to make rule no 4 more clear to cover this behavior also.
Read my post from 12/20/08 in this thread:
The call doX(1) is ambiguous as well. This looks like Rule 4 needs to be expanded, and a new rule similar to 4 added.
4. While overloading, Widening (or the identity conversion) + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
4' While overloading, vararg and unboxing + vararg can only be used in a mutually exclusive manner i.e. not together.
Jia Tan
Ranch Hand
Joined: Jan 28, 2009
Posts: 32
posted
0
I would suggest the 4th rule being changed to:
4. Combination of the following overloaded methods will result in compiler error:
a. Var-args (V) of primitive type same as argument primitive type
b. Widening + Var-args (WV)
c. Boxing/Unboxing + Var-args (BV)
Example:
Aakash Goel
Ranch Hand
Joined: May 26, 2008
Posts: 198
posted
0
what a amazing post!
just wanted to clarify one thing though:
The conversion choosing long... here is 'Un'boxing and then Widening. Right?
why is doX(Object o)preferred over the other one? one is boxing and widening and other is varargs......
Rajiv Chopra
Ranch Hand
Joined: Dec 19, 2008
Posts: 62
posted
0
Anand Shrivastava wrote:Dear Friends,
Here I found a lot of confusion with regard to autoboxing, varargs and widening. Therefore, with the help of all the ranchers and moderators I propose to compile the rules in this regard which we shall call the Golden Rules. My perception is as under, on which I invite you all for an exhaustive discussion and to incorporate any modifications, if required, so that it may help all the SCJP aspirants.
Rules :
1. Primitive Widening > Boxing > Varargs.
2. Widening and Boxing (WB) not allowed.
3. Boxing and Widening (BW) allowed.
4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
5. Widening between wrapper classes not allowed
Now Let me illustrate each of them with instances :
Dear friends, I am also a learner. So if i have made a mistake somewhere, please let me know. Moderators kindly help in this regard to consolidate the rules. Thanks in anticipation
[ July 28, 2008: Message edited by: Anand Shrivastava ]
Great Work done!!
Rajeev Kumar
SCJP 6.0 Certified 96%
Ben Power
Ranch Hand
Joined: Dec 30, 2009
Posts: 31
posted
0
Thanks, as many others said, this has been really helpful as I attempt to organize my thoughts for the SCJP (tomorrow!).
hi anand!
Better include one more rule to this post...
" A var-arg method will only be chosen if and only if no other non-var-arg method is possible. "
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: 5 Golden Rules of widening, boxing & varargs