File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another question about Widening and Boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another question about Widening and Boxing" Watch "Another question about Widening and Boxing" New topic
Author

Another question about Widening and Boxing

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121


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: 121
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: 446
    
    1

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%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121

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: 446
    
    1

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: 281


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: 121
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: 281


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: 247

Definitely >JDK 5.0 will support this...


SCJP 6 [SCJP - Old is Gold]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another question about Widening and Boxing