If I have a function that takes a double
as an argument, I can easily put in a float
.
However when I have a funcion that takes a double[]
then I cannot pass a float[]
.
public static void doSomethingWithMyDoubles(double[] doubles) {
//....
}
public static void doSomethingWithMyDouble(double doubles) {
//....
}
public static void main() {
float floater = 10f;
double doubler = 10d;
doSomethingWithMyDouble(doubler) // OK
doSomethingWithMyDouble(floater) // OK
float[] floates = new float[10];
double[] doubles = new double[10];
doSomethingWithMyDoubles(doubles) // OK
doSomethingWithMyDoubles(floates) // NOK
}
In Java, arrays are not a primitive data type; instead they are of type Object. As a consequence, you cannot convert an array of one primitive type to an array of another primitive type directly (even though you might cast the individual elements in the array to the other type).