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 Boxing Autoboxing question 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 "Boxing Autoboxing question" Watch "Boxing Autoboxing question" New topic
Author

Boxing Autoboxing question

Hari MohanSingh
Greenhorn

Joined: Dec 19, 2010
Posts: 4
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Hari MohanSingh
    Greenhorn

    Joined: Dec 19, 2010
    Posts: 4
    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
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

    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

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    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.


    Mohamed Sanaulla | My Blog
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Boxing Autoboxing question