| Author |
Question on widening ,boxing
|
Sneha Kapoor
Ranch Hand
Joined: Jun 08, 2009
Posts: 49
|
|
Hi
what does this means int doX(Number n, Number m) { return 4; }....Number is keyword or operator....I havent read about this in K&B.
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2726
|
|
|
java.lang.Number class will be on the exam. As I remembered it was mentioned in the K&B book. As from the specification "The abstract class Number is the superclass of classes java.lang.BigDecimal, java.lang.BigInteger, java.lang.Byte, java.lang.Double, java.lang.Float, java.lang.Integer, java.lang.Long, and java.lang.Short."
|
 |
reji singh
Ranch Hand
Joined: Apr 06, 2009
Posts: 52
|
|
Just to add why the output comes to be 4,3 rather than 2,3?
Thanks
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
reji singh wrote:Just to add why the output comes to be 4,3 rather than 2,3?
Thanks
Because for the short call there are only two matching argument lists:
Var-Args is always last on the list, so we go for the 4.
cheers
Bob
|
SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
|
 |
reji singh
Ranch Hand
Joined: Apr 06, 2009
Posts: 52
|
|
Thanks Bob, I didnt know the var-args priority. However, with your explanantion another doubt arises in my mind ( may be a basic one )
widening + boxing not allowed !!! why is this for short type of primitive. Usually short, char, byte all get widen to int , so method below should have called
int doX(Integer x, Integer y) { return 3; }
|
 |
Kumar Ala
Ranch Hand
Joined: Apr 30, 2009
Posts: 53
|
|
I was making some changes to the above code as below:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(int... 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));
} }
In the above code i was expecting the result as : 3 , 3 (as per the low prioirity of var-args)
but the result is : 2 , 3
Bob can you explain how the priority decides in this case
Thanks
|
kumar
SCEA5 P1
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
reji singh wrote:Thanks Bob, I didnt know the var-args priority. However, with your explanantion another doubt arises in my mind ( may be a basic one )
widening + boxing not allowed !!! why is this for short type of primitive. Usually short, char, byte all get widen to int , so method below should have called
int doX(Integer x, Integer y) { return 3; }
Widening is ALWAYS allowed , but not widening + boxing (meaning, first widening and then boxing).
So you can of course wide a byte, short or char to an int. But you CAN'T wide and box a byte, short or char to an INTEGER.
But you can box the short primitive to a Short object and wide it to a number object.
cheers
Bob
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
Kumar Ala wrote:I was making some changes to the above code as below:
class Eggs {
int doX(Long x, Long y) { return 1; }
int doX(int... 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));
} }
In the above code i was expecting the result as : 3 , 3 (as per the low prioirity of var-args)
but the result is : 2 , 3
You can't do a conversion from a primitive short to an Integer object. This is only possible through boxing + widening (in this order).
And that is not allowed. The only matching argument list in your code is the var-arg. So we have to use this method.
cheers
Bob
Ps. Please use the code brackets for the code.
|
 |
Srikanth Nakka
Greenhorn
Joined: Apr 15, 2007
Posts: 26
|
|
as i compile this code showing error as
---------- javac ----------
Eggs.java:14: reference to doX is ambiguous, both method doX(long...) in Eggs and method doX(int...) in Eggs match
System.out.print(doX(s,s) + " ");
^
1 error
if i remove comment at #1 it is compiling Properly ......
|
Thanking You,
Srikanth Nakka
|
 |
Bob Wheeler
Ranch Hand
Joined: Apr 24, 2009
Posts: 317
|
|
Srikanth Nakka wrote:
as i compile this code showing error as
---------- javac ----------
Eggs.java:14: reference to doX is ambiguous, both method doX(long...) in Eggs and method doX(int...) in Eggs match
System.out.print(doX(s,s) + " ");
^
1 error
if i remove comment at #1 it is compiling Properly ......
Yes, because then the call is not ambiguous any more. Boxing + Widening comes first, Var-Arg latest.
cheers
Bob
|
 |
 |
|
|
subject: Question on widening ,boxing
|
|
|