• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another question about Widening and Boxing

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Greenhorn
Posts: 29
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely >JDK 5.0 will support this...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic