Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

5 Golden Rules of widening, boxing & varargs

 
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 ]
 
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 ]
 
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 ]
 
author
Posts: 9000
19
  • 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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Anand
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great post, Anand!
 
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 ]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thanks Anand !
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is surely a helpful note -Thanks !
 
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these rules are really helpful !!! Good work anand
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here I am getting compiler error???
 
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 ]
 
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.
 
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.
 
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.
 
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:

 
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.
 
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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sourabhkapoorscjp Kapoor please check your private messages for some administrative matter...
 
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?



 
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.
 
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......
 
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!!
 
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
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • 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. "
 
    Bookmark Topic Watch Topic
  • New Topic