これはEclipseのバグですか?
短い変数を宣言するとき、コンパイラは整数リテラルを短いものとして扱います。
// This works
short five = 5;
ただし、整数リテラルを短いパラメータとして渡す場合は同じことを行わず、代わりにコンパイルエラーが生成されます。
// The method aMethod(short) in the type Test is not applicable for
// the arguments (int)
aMethod(5);
整数リテラルがshortの範囲外にあることを明確に認識しています。
// 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
}
}
参照:Javaプリミティブデータ型。