Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing Autoboxing question

 
Hari MohanSingh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having a hard understanding why :
final int myint = 10;
Long myLONG = myint; Compile error: cannot widen and then box. This seems OK

final short myshort = 10;
Integer iRef5 = myshort; //why does this work? is this not the same as widen and then box?

Please help

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interestingly, it is related to compile time constants... See this excerpt from the JLS.

Section 5.2 of the JLS wrote: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.



  • Basically, for compile time constants, the compiler will allow certain wrapper types, if it can confirm that the constant is within range. And from the excerpt, you can see that Long is not on that list of supported wrapper types.

    Henry
     
    Hari MohanSingh
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry,
    how does this apply?
    Long myLONG = myint;
    I am trying to widen not narrow.

    I am very new, so I might be missing something obvious.

    Thanks
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20989
    76
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hari MohanSingh wrote:
    how does this apply?
    Long myLONG = myint;
    I am trying to widen not narrow.


    It's the section that applies -- the section that talks about boxing of compile time constants. If you remove the final modifier, or anything else that makes it no longer a compile time constant, then that section no longer applies, and the code will no longer compile.

    And yes, I agree that you are not narrowing... but that wasn't the point. You can feel free to ignore the part about narrowing; you are only using the portion of the JLS that talks about the boxing of compile time constants (Maybe I wasn't clear enough about this, in my previous post, sorry).


    Anyway, regarding whole number constants, they only come in two flavors -- integer and long. So, your variable, myShort, is actually an integer constant. And your code that works, is simply boxing.

    Henry
     
    Mohamed Sanaulla
    Saloon Keeper
    Posts: 3159
    33
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hari MohanSingh wrote: final int myint = 10;
    Long myLONG = myint; Compile error: cannot widen and then box. This seems OK

    final short myshort = 10;
    Integer iRef5 = myshort; //why does this work? is this not the same as widen and then box?

    Did you try compiling those two statements?

    Update: Added the Quote.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic