FULL PRODUCT VERSION :
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux bluejws1 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:04:26 UTC 2009 i686 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
have been reading the Java Language Spec, 3rd edition, and have found what I think is a discrepancy between the spec and the javac compiler implementation.
Section 15.16 talks about cast expressions. It says that it should be a compile time error if the argument type cannot be converted to the cast type via casting conversion (section 5.5):
"It is a compile-time error if the compile-time type of the operand may never be cast to the type specified by the cast operator according to the rules of casting conversion (§5.5). Otherwise, at run-time, the operand value is converted (if necessary) by casting conversion to the type specified by the cast operator."
Section 5.5 talks about casting conversion. It gives a list of conversion types which are allowed. Specifically absent from the list is "unboxing conversion followed by widening/narrowing primitive conversion". However that exact sequence of conversions does seem to be allowed by the javac compiler. For instance:
long l = (long)(Integer)45;
... compiles just fine. (The problematic cast is the cast to long; the argument is of type java.lang.Integer, so the conversion requires unboxing to int followed by a widening primitive conversion).
Likewise, according to the JLS it should not be possible to cast from byte to char, because that (according to 5.1.4) requires a widening primitive conversion and a narrowing primitive conversion - however, this cast is also allowed by the compilers.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Write source code with a cast requiring unboxing conversion followed by widening primitive conversion
2. Compile the source with javac
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation error!
ACTUAL -
No compilation error.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class JavacTest
{
public void aMethod()
{
long l = (long)(Integer)45;
char c = (char)(byte)33;
}
}
---------- END SOURCE ----------
java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) Server VM (build 19.1-b02, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux bluejws1 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:04:26 UTC 2009 i686 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
have been reading the Java Language Spec, 3rd edition, and have found what I think is a discrepancy between the spec and the javac compiler implementation.
Section 15.16 talks about cast expressions. It says that it should be a compile time error if the argument type cannot be converted to the cast type via casting conversion (section 5.5):
"It is a compile-time error if the compile-time type of the operand may never be cast to the type specified by the cast operator according to the rules of casting conversion (§5.5). Otherwise, at run-time, the operand value is converted (if necessary) by casting conversion to the type specified by the cast operator."
Section 5.5 talks about casting conversion. It gives a list of conversion types which are allowed. Specifically absent from the list is "unboxing conversion followed by widening/narrowing primitive conversion". However that exact sequence of conversions does seem to be allowed by the javac compiler. For instance:
long l = (long)(Integer)45;
... compiles just fine. (The problematic cast is the cast to long; the argument is of type java.lang.Integer, so the conversion requires unboxing to int followed by a widening primitive conversion).
Likewise, according to the JLS it should not be possible to cast from byte to char, because that (according to 5.1.4) requires a widening primitive conversion and a narrowing primitive conversion - however, this cast is also allowed by the compilers.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Write source code with a cast requiring unboxing conversion followed by widening primitive conversion
2. Compile the source with javac
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Compilation error!
ACTUAL -
No compilation error.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class JavacTest
{
public void aMethod()
{
long l = (long)(Integer)45;
char c = (char)(byte)33;
}
}
---------- END SOURCE ----------
- relates to
-
JDK-6526446 Fixes to JLS5.5 Casting Conversion
- Closed