4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
Stephan van Hulst wrote:Calling the Object... method does not entail widening, only boxing.
Stephan van Hulst wrote:First the byte is boxed to a Byte. A Byte is a subclass of Object, so there is no widening. Widening only happens between primitives.
Well, in this case I am referring to conversion with the same word, widening, in the sense that a byte gets converted/widened to a Number/Object, after it has first been boxed to a Byte.
Stephan van Hulst wrote:Having go(byte... a) and go(Number... a) in the same class compiles fine for me.
[edit]
Well, in this case I am referring to conversion with the same word, widening, in the sense that a byte gets converted/widened to a Number/Object, after it has first been boxed to a Byte.
Well, you may think I'm nitpicking, you shouldn't refer to this as widening, because there is no conversion, and nothing gets widened. It's merely an upcast.
Widening is an actual conversion where the value of a primitive is changed into an equivalent value of a larger primitive type. When you cast one object reference to a supertype, nothing gets changed.
Andreas wrote:An argument of type byte or Byte wil always be ambiguous if: (excluding Object...) the only methods that match are 2 or more vararg methods, AND atleast one of the methods is a primitive vararg method.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ikpefua Jacob-Obinyan wrote:The following program does NOT approve of the above, it compiles!
Stephan van Hulst wrote:
Ikpefua Jacob-Obinyan wrote:The following program does NOT approve of the above, it compiles!
Because it's not ambiguous. You can't Widen + Box, so Integer... *never* applies to byte.
Stephan van Hulst wrote:Okay Andreas, I now tested using javac. I think you will agree to these hard and fast rules:
1) Everything > Object...
2) Single > Varargs
3) Widening + Boxing; not considered
4) Varargs = (Varargs + Widening) = (Varargs + Boxing) = (Varargs + Unboxing); ambiguous
5) Widening > Boxing
6) Upcasting > Unboxing
7) Specific upcasting > General upcasting
Widening and boxing apply only to primitives. Unboxing and upcasting apply only to wrappers.
[edited a million times for typos]
Andreas wrote:Indeed, you had me thinking "what the h*ll" there for a moment Jacob But like Stephan pointed out, this is just not an ambiguous method call, Integer is never a valid option here
Stephan wrote:"Everything > Object..., except if the argument is Object[]"
Stephan wrote:
At this moment I'm starting to get *really* curious what the JLS has to say about overloading.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Andreas Svenkson wrote:
byte => int > Byte > byte...
reads: "byte will be widened to another primitive before it is boxed - and it will be boxed and widened as a wrapper, before it matches a primitive vararg or a wrapper vararg"
Byte => Number > byte > Byte...
reads: "Byte will widened to a superclass before it is unboxed - and it will be unboxed and widened as a primitive, before it matches a pimitive vararg or wrapper vararg"
Stephan van Hulst wrote:Calling the Object... method does not entail widening, only boxing.
First the byte is boxed to a Byte. A Byte is a subclass of Object, so there is no widening. Widening only happens between primitives.
You can box and then widen. (An int can become an Object, via Integer.)
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stephan van Hulst wrote:Hmm it seems the language specification indeed uses the term widening, instead of upcasting.
So to clarify my above posts, I used the term "widening" to refer to primitive widening *only*. I use the term upcasting to refer to object reference widening *only*.
You are not allowed to widen a byte to an int, and then box to an Integer.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Andreas wrote:PS: Where did you learn about the implicit casting of compound assignments? I can't seem to find it in K&B.
In fact, +=, -=, *=, and /= will all put implicit cast.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Andreas Svenkson wrote:Indeed there it was, I had even underlined it and yet I missed it yesterday evening... Just a good example of that studying too late in the evening isn't such a good thing
SCJP 6. Learning more now.
OCPJP 6.0
Ikpefua Jacob-Obinyan wrote:@Andreas...I have just CONFIRMED my exams for Monday June 20...So I will sort of coordinate myself with you, it is the 5th time I am going through the K & B book, I will be through on Friday and do mock tests Saturday and Sunday, Then Monday I will go there and get-the-job-done...Wish me luck!...
OCPJP 6.0
Achilleas wrote:I have written notes on each one of the 9 chapters + one additional of mine + over 140 programs. If you are interested i can send it all over.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Rahul wrote:recite them regularly like a parrot.
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Consider Paul's rocket mass heater. |