JDK-4995668 : Narrowing with autoboxing needs an extra cast
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-02-17
  • Updated: 2006-02-12
  • Resolved: 2006-02-10
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
Name: gm110360			Date: 02/17/2004


FULL PRODUCT VERSION :
build 1.5.0-beta-b32c, mixed mode

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
This is related to 4974939 and 4920452. But these only report about converting from literals, and don't mention the extra cast workaround.

Narrowing type conversion during autoboxing requires an extra cast.:

         // These
        Long lw= (long) new Integer(7);
        Integer iw=(int) (long) new Long(3L);
        long d = (long) 6;
        int f = (int) 7L;
         // work well, while:
        iw=(int) new Long(3L);
        // doesn't compile


        Double dw= (double) new Float(1.2F);
        Float fw=(float) (double) new Double(3.2);
        double d = (double) 6.7F;
        float f = (float) 6.0;

        fw=(float) new Double(4.3);   doesn't compile


        Double dw=(double) new Integer(3);
        Integer iw= (int) (double) new Double(3.2);
        double d = (double) 6;
        int i = (int) 7.5;

        iw= (int) new Double(3.2);  doesn't compile




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Casting once world be great.
ACTUAL -
Compile time error

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Adding the extra cast, since casting primitives works fine.
(Incident Review ID: 239114) 
======================================================================

Comments
EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=11376&forumID=1463
12-02-2006

EVALUATION ###@###.### writes: The specification says: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5 "A value of a reference type can be cast to a primitive type by unboxing conversion (��5.1.8)." And section 5.1.8 specifies that there are only 8 unboxing conversions: * From type Boolean to type boolean * From type Byte to type byte * From type Character to type char * From type Short to type short * From type Integer to type int * From type Long to type long * From type Float to type float * From type Double to type double Consequently, there is no unboxing conversion from Long to int. This is a good thing as it can cause loss of precision.
10-02-2006