这是Eclipse中的错误吗?
当声明一个短变量时,编译器将整数文字视为短。
// This works
short five = 5;
然而,当将整数文字作为短参数传递时,它不会做同样的事情,而是生成编译错误:
// The method aMethod(short) in the type Test is not applicable for
// the arguments (int)
aMethod(5);
它清楚地知道整数文字何时超出短期范围:
// Type mismatch: cannot convert from int to short
short notShort = 655254
-
class Test {
void aMethod(short shortParameter) {
}
public static void main(String[] args) {
// The method aMethod(short) in the type Test is not applicable for
// the arguments (int)
aMethod(5);
// the integer literal has to be explicity cast to a short
aMethod((short)5);
// Yet here the compiler knows to convert the integer literal to a short
short five = 5;
aMethod(five);
// And knows the range of a short
// Type mismatch: cannot convert from int to short
short notShort = 655254
}
}
答案 0 :(得分:8)