• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hey kathy S - Can you widen a wrapper class or not? !!!  RSS feed

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy's a bit busy and hasn't dropped in recently, is anyone else allowed to respond?
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, i'll dare to answer:

It is possible to widen Byte, Short, Integer and Long to Number or even to Object, but it is impossible to widen Byte to Short, Short to Integer and so on...
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that Number isn't a wrapper. It's the superclass of all numeric wrappers.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hank you for your responses.

So if I am understanding this correctly, then Number is the grandaddy superclass to the primitive number wrapper classes and therefore Byte, Integer, Short etc can widen to a Number.

However those same subclasses of Number cannot widen to each other because they are siblings of each other.

Correct?
thanks
G
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!