-
Bug
-
Resolution: Fixed
-
P5
-
5.0
-
None
-
rc
-
generic
-
generic
-
Verified
According to 5.2 (assignment conversions) the second shouldn't be allowed, as it would be a widening reference conversion followed by an unboxing conversion, which isn't among the allowed conversions. However, I believe that is a bug in the JLS. (One could infer that T must be Integer, as Integer is final, but the type rules don't take advantage of final). A widening reference conversion followed by unboxing isn't listed among the conversions allowed in the corrections in http://java.sun.com/docs/books/jls/jls-proposed-changes.html . Perhaps Alex will get it fixed in the next round.
-Neal
On 5/1/07, ... wrote:
I'm curious about this case:
class A<T extends Integer> {
T x = (T)37; // javac gives error
int i = (int)x; // javac gives no error
}
javac gives an error on the first case but not the second. The JLS says
in 5.5:
A value of a primitive type can be cast to a reference type by
boxing conversion (§5.1.7).
A value of a reference type can be cast to a primitive type by
unboxing conversion (§5.1.8).
It's not clear whether that means one of those conversions plus
something else.
If I read it as only boxing or unboxing, the error on the first case
makes sense.
But then I would also expect an error on the second case.
Which way is right? Is there a javac bug here on one of these cases?
-Neal
On 5/1/07, ... wrote:
I'm curious about this case:
class A<T extends Integer> {
T x = (T)37; // javac gives error
int i = (int)x; // javac gives no error
}
javac gives an error on the first case but not the second. The JLS says
in 5.5:
A value of a primitive type can be cast to a reference type by
boxing conversion (§5.1.7).
A value of a reference type can be cast to a primitive type by
unboxing conversion (§5.1.8).
It's not clear whether that means one of those conversions plus
something else.
If I read it as only boxing or unboxing, the error on the first case
makes sense.
But then I would also expect an error on the second case.
Which way is right? Is there a javac bug here on one of these cases?
- relates to
-
JDK-6526446 Fixes to JLS5.5 Casting Conversion
- Closed
-
JDK-6578574 short should convert to Integer under JLS 5.2
- Open
-
JDK-8166326 5.2: Allow widening before unboxing
- Closed
-
JDK-8166396 5.5: Clean up description of legal conversions in casts
- Closed