This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Question 29: What is the result of executing the following code:
Select the one right answer. a. 1 b. 2 c. 3 d. 4 e. 5 Ans: C Explination: Question 29: c. The method with all double parameters is actually the only version of test() that the Java Virtual Machine can legally coerce the numbers to. The reason the other versions of test() are not invoked is that at least one of the parameters would have to be automatically coerced from a type with greater accuracy to a type with less accuracy, which is illegal. (See chapter 9.) I still did not understand...Infact I thought the ans would be (a).
Hey! It compiles! Ship it!
Joined: Feb 11, 2003
Should have thought of this earlier. The 3rd arg is by default an int and so it can be passed into the 3rd method which takes all 3 doubles. Can't go to the 1st method which has the 3rd arg as short. So the double, long and int are passed to the the double-double-double method...Right?
I would think C is correct because it is the only method where the parameters could be widened in all cases. In all of the other cases the parameter would have to be narrowed which the compiler won't do for you without an explicit cast.