Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 5 Golden Rules of widening, boxing & varargs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "5 Golden Rules of widening, boxing & varargs" Watch "5 Golden Rules of widening, boxing & varargs" New topic
Author

5 Golden Rules of widening, boxing & varargs

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
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 ]


Anand Shrivastava
SCJA
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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
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.

Thanks.
[ July 28, 2008: Message edited by: Anand Shrivastava ]
Jean-Luc Thirion
Greenhorn

Joined: Jan 24, 2008
Posts: 19
Does that imply that the combination "boxing + Object Widening" is ok also for assignement ?

For instance, I think this will compile (I am quite sure):



Sorry, I know I can test that but I am not at home right now.
[ October 22, 2008: Message edited by: Jean-Luc Thirion ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
I didn't verify the technical details, but such a chart is a GREAT idea!

Also Anand, you can edit away those multiple edit notes on the bottom of your first post.

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Great Anand


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Great post, Anand!


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Nice research Anand

One more example for (Boxing+Widening) is preferred over (Boxing+Varargs).

doX(Integer... i) doX(Object o) doX(5) (Object o) based of rule 1, 3.
[ December 18, 2008: Message edited by: punit singh ]

SCJP 6
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Yeah thanks Anand !


SCJP 5
Jayati Das
Greenhorn

Joined: Feb 01, 2007
Posts: 18
This is surely a helpful note -Thanks !


SCJP5 85%
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
doX(Long l) & doX(long...l) Integer i; long...l(Rule 5 & 1) doX(i)

In this is it following the series as unboxing-widening-varargs?

Thanks,

Geeta Vemula
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
yeah, it is following unboxing+widening+varargs.
aparna shinde
Greenhorn

Joined: Dec 08, 2008
Posts: 11
these rules are really helpful !!! Good work anand


Regards,<br />aparna
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


Here I am getting compiler error???
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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
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
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
how it is processed for linr no8
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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
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
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
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
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
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
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
doX(int... i) & doX(long... l), call doX(5) get ambiguous too
Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
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):

doX(int... i) & doX(long... l)
doX(int... i) & doX(float...f)
doX(long... l) & doX(double... d)
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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
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
what a amazing post!

just wanted to clarify one thing though:



The conversion choosing long... here is 'Un'boxing and then Widening. Right?



SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Aakash Goel wrote:what a amazing post!

just wanted to clarify one thing though:



The conversion choosing long... here is 'Un'boxing and then Widening. Right?




Yes you are right.
Sourabhkapoor Kapoor
Greenhorn

Joined: Oct 09, 2009
Posts: 10
public class xt{

public static void main(String argv[]){
int i=2;
doNumber(i);
}

public static void doNumber(int... dn){ //line 4
System.out.print("c");
}


public static void doNumber(Integer... dn){ //line 5
System.out.print("b");
}

public static void doNumber(Object dn){ line 6
System.out.print("e");
}

}


Answer: e
Modified rule 4 needs to change i think..Please correct me If i am wrong

line 4:rule 4(a)
line 5 :rule boxing var-args 4(c)
Still can compile no ambiguity cuz line 6 will help the compiler to resolve ambiguity.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Sourabhkapoorscjp Kapoor please check your private messages for some administrative matter...
zhong chen
Greenhorn

Joined: Oct 12, 2009
Posts: 24
Some addition to this:

zhong chen
Greenhorn

Joined: Oct 12, 2009
Posts: 24
I found this in Examlab mockup 1.

Why this code compiles without error? Aren't these static methods overloaded?



Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Yes, they are overloaded but why do you think the code should result in compile time error.


Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
doX(Object o) & doX(int...i)

doX(5);

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
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
Thanks, as many others said, this has been really helpful as I attempt to organize my thoughts for the SCJP (tomorrow!).

Ben


SCJP 6
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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. "
 
 
subject: 5 Golden Rules of widening, boxing & varargs
 
Similar Threads
method invocation
SCJP problem with boxing
Var args ... explanation needed
only 8 days for exam.....
WidenBoxing Confusion