これは、IDE (この場合は誤解を招く可能性があります) がオートボクシングの可能性を認識しているためです。
たとえば、 を作成した場合、List<Integer>
それに を追加できます。 からへのint
オートボクシングがあります。Unxobing は逆です:へ。ボックス化とボックス化解除の両方が数値プリミティブ型にのみ適用されることに注意してください (ただし、それらの配列には適用されません)。int
Integer
Integer
int
ここでは、 のメソッドが最終的に選択されることは間違いありませんdouble
(より具体的であるため) が、IDE はあいまいさが存在する可能性があると見なします。
このコード例:
public final class Test
{
public static void item(Object a, Object b, String c, String d)
{
System.out.println("Object");
}
public static void item(double a, double b, String c, String d)
{
System.out.println("double");
}
public static void unbox(final double d)
{
System.out.println("Unboxed!");
}
public static void useIt(double a, double b, Double c, Double d)
{
// primitives
item(a, b, "", "");
// cast to corresponding classes
item((Double) a, (Double) b, "", "");
// objects
item(c, d, "", "");
// unboxed by the "unbox" method which takes a "double" as an argument
unbox(new Double(2.0));
}
public static void main(final String... args)
{
// Autoboxing of the third and fourth argument
useIt(1.0, 1.0, 1.0, 1.0);
}
}
出力:
double
Object
Object
Unboxed!
ただし、次の呼び出しはできないことに注意してください。
useIt((Double) a, b, c, d); // No autoboxing of "b"