| Author |
Another question about Widening and Boxing
|
Gary Marshall
Ranch Hand
Joined: Feb 19, 2007
Posts: 120
|
|
With this code the compiler complains that "The method doX(Integer, Integer) in the type Eggs is not applicable for the arguments
(short, short)".
What I am thinking is that the compiler widened the 's' short variables to two integers, but it does not (will not?) also box these two integers to two Integers. Is this correct? Is it true that the compiler will NOT box a variable after it has just widened it? That is, will the compiler do one or the other, not both?
Thank you for your time.
Gary
|
G
|
 |
saikiran venkata
Greenhorn
Joined: Oct 31, 2010
Posts: 29
|
|
widening followed by boxing is not allowed......
boxing followed by widening is allowed........,
short --> int --> Integer is not allowed
short -->Short --> Object is allowed.
short -->Short --> Integer is not allowed because they are siblings, not a valid widening.
|
SCJP 1.6 | SCWCD 5
The earth is enjoyed by heroes—this is the unfailing truth. Be a hero. Always say, “I have no fear." ~Swami Vivekananda.
|
 |
Gary Marshall
Ranch Hand
Joined: Feb 19, 2007
Posts: 120
|
|
Thank you for your response
boxing followed by widening is allowed........,
and
short -->Short --> Integer is not allowed because they are siblings, not a valid widening.
So does this mean that a wrapper object could only be widened to an Object? If thats true, I wonder why one would want to do so? hmmm...
Thanks again
Gary
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
It is not necessary that all the Wrappers will be widened to Object class
Wrapper classes are subclasses of class Number
so they can be widened to class Number also
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Gary Marshall
Ranch Hand
Joined: Feb 19, 2007
Posts: 120
|
|
OK. Thank you
Given this code:
Result is " 4 3"
I wonder why, since widening is favored over boxing, and that the JVM uses the method with the smallest argument that is wider than the parameter, why didn't the compiler/JVM do this:
short --> Widen to int ==> select the " int doX(long... x)" method. (BTW: This is the method that is selected when the "int doX(Number n, Number m) " method is commented out).
Instead, the compiler/JVM looks like it did this:
short --> Boxed short to Short --> Widened to Number ==> selected the "int doX(Number n, Number m) " method.
Why would it do this if Widening "beats" Boxing?
thanks
Gary
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
Gary Marshall wrote:
short --> Widen to int ==> select the " int doX(long... x)" method. (BTW: This is the method that is selected when the "int doX(Number n, Number m) " method is commented out).
Always remember that variable arguments is a new concept and to keep the old code in java safe, the var- args are always chosen at last so here, boxing will be preferred
Widening Beats Boxing applies when they are used separately
see the following code
now look the following code
Widening beats boxing when there are two overloaded methods in which one has just Widening parameters and another has Boxing parameters
Hope this helps
hth
|
 |
Javin Paul
Ranch Hand
Joined: Oct 15, 2010
Posts: 276
|
|
Interesting thread but got little confused between priority of widening and boxing .
just to make it clear if both widening and boxing is applicable for any place I see widening is happening before boxing as per above example.
is that true for all jdk compiler or does it varies ?
Thanks
Javin
|
http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
|
 |
Gary Marshall
Ranch Hand
Joined: Feb 19, 2007
Posts: 120
|
|
Javin Paul wrote:
just to make it clear if both widening and boxing is applicable for any place I see widening is happening before boxing as per above example.
is that true for all jdk compiler or does it varies ?
Thanks
Javin
Don't take my response as final but I do know that Boxing came on the scene as of Java 5. So I'm thinking your results will be different vs what you see in this thread if you are using a pre-Java5 jdk compiler.
Gary
|
 |
Javin Paul
Ranch Hand
Joined: Oct 15, 2010
Posts: 276
|
|
Hi Gary,
Pre JDK 5.0 is OK , Since there is no boxing so no question of priority between widening and boxing , I am asking about JDK 5.0 and above ?
Thanks
Javin
|
 |
Ram Narayan.M
Ranch Hand
Joined: Jul 11, 2010
Posts: 244
|
|
|
Definitely >JDK 5.0 will support this...
|
SCJP 6 [SCJP - Old is Gold]
|
 |
 |
|
|
subject: Another question about Widening and Boxing
|
|
|