• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

widening, narrowing, boxing...

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe there is a anthwer to this post allready somewhere, but i did�nt find...

Short s = 5;
this is ok, cause narrowing a compileTimeConstant and than boxing is ok

Short s2 = (byte)5;
this is ok, because again its a compileTimeConstant, so in this case even widening and than boxing is ok (what it is not in other cases, e.g.

Byte b = (byte)5;
Short s3 = b; //Not OK Widening than Boxing

But why is the following NOT ok?
Like in the 2nd example i`m widening and than boxing a compileTimeConstant

Long l = 5;

The more i try to understand this stuff , the more i get confused maybe after all i dont see the tree in the woods?

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

It's admittedly unintuitive, but the issue here is that Java does not allow for automatic widening followed by boxing, even for compile-time constants. It does allow automatic narrowing following by boxing, as you've noted. You can take a look at the Java Language Specification to see the details.

For the same reason, you also can't write "Double d = 5", even though "double d = 5" is legal.
 
Peter Ricke
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo Kelvin,
thank you for the anthwer, but...

if

It [java] does allow automatic narrowing following by boxing


why can`t i compile
Float f = 5.0; //not ok
from my point of view, i`m trying here to narrow and than box.
OK, maybe double cannot be narrowed to float but
Integer i = 3L; also does not work. (Narrowing also not possible here?)

and if

Java does not allow for automatic widening followed by boxing, even for compile-time constants


why does my 2nd example
Short s2 = (byte)5;
compile ? I am widening (from byte to short) or am I not?

Hopefully yours...
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

You're right; my previous answer was not sufficiently precise. Again, I refer you to the JLS for the full set of rules that Java applies. But here are clarifications for your specific questions:

Originally posted by Peter Ricke:
why can`t i compile
Float f = 5.0; //not ok
from my point of view, i`m trying here to narrow and than box.
OK, maybe double cannot be narrowed to float but
Integer i = 3L; also does not work. (Narrowing also not possible here?)


The automatic narrowing followed by boxing applies only to variables of type Byte, Short, and Character. Here's the rule stated in the JLS:

In addition, if the expression is a constant expression (�15.28) of type byte, short, char or int :

- A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
-A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :

  • Byte and the value of the constant expression is representable in the type byte.
  • Short and the value of the constant expression is representable in the type short.
  • Character and the value of the constant expression is representable in the type char.


  • why does my 2nd example
    Short s2 = (byte)5;
    compile ? I am widening (from byte to short) or am I not?


    This seems to be an application of the same rule I quoted above, though I admit that I'm not 100% sure of this. I'll see if I can look up a more precise explanation.
    [ December 13, 2007: Message edited by: Kelvin Lim ]
     
    Peter Ricke
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you again,

    at least one half of the confusion is gone.
    It seems that it is possibly necessary just to memorise the other half;-)

    Regards,
     
    Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic