• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

5 Golden Rules of widening, boxing & varargs

 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Likes 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Anand
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great post, Anand!
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks Anand !
 
Jayati Das
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is surely a helpful note -Thanks !
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, it is following unboxing+widening+varargs.
 
aparna shinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these rules are really helpful !!! Good work anand
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here I am getting compiler error???
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone clear about line no 9 in anand's post
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how it is processed for linr no8
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doX(int... i) & doX(long... l), call doX(5) get ambiguous too
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what a amazing post!

just wanted to clarify one thing though:



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


 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sourabhkapoorscjp Kapoor please check your private messages for some administrative matter...
 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some addition to this:

 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this in Examlab mockup 1.

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



 
Atwal Usha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they are overloaded but why do you think the code should result in compile time error.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Ben Power
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, as many others said, this has been really helpful as I attempt to organize my thoughts for the SCJP (tomorrow!).

Ben
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. "
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic