似乎允许的转换已在 Java 5 和 7 之间进行了扩展。在Java Language Specification, 3rd edition (for Java 5 and 6) 中:
可以通过拆箱转换(第 5.1.8 节)将引用类型的值强制转换为原始类型。
拆箱转换将引用类型的值转换为原始类型的对应值。具体来说,以下 8 种转换称为拆箱转换:
- 从布尔类型到布尔类型
- 从字节类型到字节类型
- 从字符型到字符型
- 从短类型到短类型
- 从 Integer 类型到 int 类型
- 从 Long 类型到 long 类型
- 从浮点型到浮点型
- 从 Double 类型到 double 类型
所以在 Java 5 和 6 中,强制转换Object
为double
是不合法的。
Java 语言规范,Java SE 7 版写道:
The following tables enumerate which conversions are used in certain casting conversions. Each conversion is signified by a symbol:
⇓ signifies narrowing reference conversion (§5.1.6)
⊔ signifies unboxing conversion (§5.1.8)
and the following table says a cast from Object to double to be
⇓,⊔</p>
i.e. a cast from Object
to double
is a cast to Double
followed by an unboxing conversion to double
.
It is therefore very likely that your team mates are compiling for Java 7, while you are compiling for Java 6.