Bert Bates wrote:Hey Guys,
Here's a 'thought experiment' that might help explain some of this behavior...
Consider that before Java 5 there was widening but not boxing. So, if you don't want old, working code to break, and you want to add boxing, what rules would you have to create?
hth,
Bert
Hi Bert and thanks for taking the time to post a reply. I totally agree that it's better to have us think through an answer to our questions rather than just giving us the answer. We're sure to remember and learn much better that way.
But for this one I really have to say that I just can't figure out why the behaviour in the original question is present.
To prevent old code from breaking you would need:
- widening should always win over boxing when a choice exist (lifted from your book, p.249)
But I can't figure out how this 'rule' creates the conditions that allow an int to be passed to method that expects a Short, but cause a compilation error when you try to declare and initialize a Short using an int.
If Short s = 4; is valid code then I would expect Car().test(4) to be also. But obviously I'm wrong