In chapter three of her book SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) Katherine Sierra covers widening of wrapper classes. At the end of the chapter there are test questions. Here is question number 5:
Given:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }
What is the result?
A. 1 1
B. 2 1
C. 3 1
D. 4 1
E. 2 3
F. 3 3
G. 4 3
Katherine states the correct answer is "G" and her reason is that Java 5 cannot handle widening followed by boxing. She reasons: "....But you can box shorts to Shorts and then widen them to Numbers....".
I am confused about this because earlier in the chapter she states "...it's not legal to widen from one wrapper class to another, because the wrapper classes are peers to one another. For instance, it's NOT valid to say that Short IS-A Integer....", and she states "...Remember, none of the wrapper classes will widen from one to another! Bytes won�t widen to Shorts, Shorts won�t widen to Longs, etc....".
And this is the reason why I am posting here. I am confused. Can you widen a wrapper class or not? Perhaps I am not understand her answer as it applys within the scope of the question. After all, I am a java greenhorn.
Can one of you Java gurus help a green horn understand what Katerhine is trying to explain to me? Is there in fact a contradiction here or do I need stronger reading glasses?
I hope I don't get in trouble for cutting and pasting from the book to this forum, and let it be known that I like Katherine's SCJP prep book and am diligently working through it in prep for that cert. Its a good book which focuses only on what you need to know to pass the cert, which is what my objective is at this time.
Thank you Katherine and thanks to all for your time in responding...
G
Given:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(long... x) { return 2; }
int doX(Integer x, Integer y) { return 3; }
int doX(Number n, Number m) { return 4; }
public static void main(String[] args) {
new Eggs().go();
}
void go() {
short s = 7;
System.out.print(doX(s,s) + " ");
System.out.println(doX(7,7));
} }
What is the result?
A. 1 1
B. 2 1
C. 3 1
D. 4 1
E. 2 3
F. 3 3
G. 4 3
Katherine states the correct answer is "G" and her reason is that Java 5 cannot handle widening followed by boxing. She reasons: "....But you can box shorts to Shorts and then widen them to Numbers....".
I am confused about this because earlier in the chapter she states "...it's not legal to widen from one wrapper class to another, because the wrapper classes are peers to one another. For instance, it's NOT valid to say that Short IS-A Integer....", and she states "...Remember, none of the wrapper classes will widen from one to another! Bytes won�t widen to Shorts, Shorts won�t widen to Longs, etc....".
And this is the reason why I am posting here. I am confused. Can you widen a wrapper class or not? Perhaps I am not understand her answer as it applys within the scope of the question. After all, I am a java greenhorn.
Can one of you Java gurus help a green horn understand what Katerhine is trying to explain to me? Is there in fact a contradiction here or do I need stronger reading glasses?
I hope I don't get in trouble for cutting and pasting from the book to this forum, and let it be known that I like Katherine's SCJP prep book and am diligently working through it in prep for that cert. Its a good book which focuses only on what you need to know to pass the cert, which is what my objective is at this time.
Thank you Katherine and thanks to all for your time in responding...
G