• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusing var-arg constructors

 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question inspired by Devaka's ExamLab question:

The original question was:

And it went ok, we have no constructor here, so get 0 in output.

Then i tried to play with constructors:

if i have one of them constructors:

or

then the code compile just fine, but when both of them are in code - then compiler doesn't compile this if i try to instantiate the class object.


I am a bit confused with the behavior, though i understand that normally no one would want to use all those constructors together.

Can one explain me the rules that stand behind this behavior?

PS found in another code example that we cannot use primitive and wrapper var-arg methods together. I think that solved my question - we can actually have those var-arg methods in our code as long as we don't try to use them - in that case compiler gets confused with ambiguous choice. (if in the Devaka's example we try to ct.Centrist(1,1); - we'll get the same compiler error)
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What definitely wont compile is This is because varags are (well, how I think of them) arrays created on the fly by the JVM. So these are ambiguous constructors.

Now...as to the instantiation in your example; its because of autoboxing I think.This gives you an int[] at run time. That could apply to int[]/int... or Integer[]/Integer...This shows exactly the same issue. I am not really sure if autoboxing was a dumb idea - or if I am too stupid to follow the logic behind it. I'll stop not before I start ranting and raving!
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for helping me to sort this out 8)
 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Jason you are right. int... and Integer... are same (from Java 5 onwards) due to autoboxing
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that matter char... and Integer... too can't go together. Or stated in a broader way, any types where "boxing followed by widening" is conflicting, cannot go together.

Regards
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:For that matter char... and Integer... too can't go together. Or stated in a broader way, any types where "boxing followed by widening" is conflicting, cannot go together.

Not exactly right - only the primitive and its corresponding wrapper type can not be used together in var-arg methods. Since literal value of integer parameter is int by default if you dont't cast it explicitly to byte, its absolutely not confusing for constructor which method to use:




But this code won't compile:
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anastasia Sirotenko wrote:
Himanshu Kansal wrote:For that matter char... and Integer... too can't go together. Or stated in a broader way, any types where "boxing followed by widening" is conflicting, cannot go together.

Not exactly right - only the primitive and its corresponding wrapper type can not be used together in var-arg methods. Since literal value of integer parameter is int by default if you dont't cast it explicitly to byte, its absolutely not confusing for constructor which method to use:




But this code won't compile:


You are not using "empty" constructors. Using that, even byte... and Long... wont compile together. So what I said was exaclty right.

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic