Originally posted by Anand Shrivastava:
Nabila Thanks,
Just it came to my mind, what we are upto,
ultimately we are doing,
Number = Short
Number is a superclass.
or in otherwords,
Superclasstype = Subclass object,
this is upcasting,
infact we are narrowing it (so the term boxing and narrowing should have been used
I am not sure how you are coming to the conclusion that we are narrowing it.
We are not narrowing the supertype Number to Short.
If you can be more clear may be I can help you.
The first statement that is executed is from the main.
s is assigned the value 7
short s=7 The next statement is
System.out.print(dox(s,s) + " "); It has 4 options.
1. static int dox(Long x, Long y) { return 1;} Widening and boxing is not possible - short cannot be wident to long and boxed to Long. so this option is discarded.
2.static int dox(long...x) {return 2;}This option will be the last one to be considered as it is var arg.
3.static int dox(Integer x, Integer y) { return 3;}This has the same case as the first one.
Widening and boxing is not possible - short cannot be wident to int and boxed to Integer. so this option is discarded.
4.static int dox(Number n, Number m) {return 4;}This statement is possible .
The
short s= 7 is boxed to
Short s=7 And this Short is then widend to Number which is a parent class of the wrapper classes.
so the first out put is 4 In teh second case
System.out.println(dox(7,7)); 7 is implicitly an
int and so the best option is simply boxing it to
Integer.And thus the output is 3.
I hope you are clear with it now.
[ July 24, 2008: Message edited by: Nabila Mohammad ]