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 ]
This message was edited 1 time. Last update was at by Ulf Dittmer
Anand Shrivastava<br />SCJA
Milan Sutaria
Ranch Hand
Joined: Jul 10, 2008
Posts: 107
posted
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)
Cleared SCJP 6 83%
Anand Shrivastava
Ranch Hand
Joined: Jul 22, 2007
Posts: 106
posted
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: 950
posted
yeah, it is following unboxing+widening+varargs.
aparna shinde
Greenhorn
Joined: Dec 08, 2008
Posts: 11
posted
these rules are really helpful !!! Good work anand
Regards,<br />aparna
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
posted
Here I am getting compiler error???
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
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
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: 950
posted
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
how it is processed for linr no8
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 950
posted
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
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
This message was edited 3 times. Last update was at by Jose Francisco Ruiz Massieu
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
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
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: 950
posted
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
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:
This message was edited 1 time. Last update was at by Ulf Dittmer
Fiona
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 950
posted
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
doX(int... i) & doX(long... l), call doX(5) get ambiguous too
Jia Tan
Ranch Hand
Joined: Jan 28, 2009
Posts: 32
posted
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):
This message was edited 2 times. Last update was at by Jia Tan
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
Punit Singh wrote: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.
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
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: 197
posted
what a amazing post!
just wanted to clarify one thing though:
The conversion choosing long... here is 'Un'boxing and then Widening. Right?
This message was edited 1 time. Last update was at by Aakash Goel
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
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
Thanks, as many others said, this has been really helpful as I attempt to organize my thoughts for the SCJP (tomorrow!).
Ben
SCJP 6
subject: 5 Golden Rules of widening, boxing & varargs