I've been writing Java for a while and have even been starting to teach it to others. I find it hard to explain to a new student why a float array's values must be casted. For example:
float[] someArray = {(float) 23.23, (float) 123.1, (float) 123.1};
int[] intArray = {12, 13, 4, 5};
double[] doubleArray = {22.12, 23.1, 12.1};
I'm guessing that the the values that include decimals are just treated as Doubles rather than floats, hence the need for casting. What is the reason that Java chooses it to be this way, couldn't the compiler also figure that because it is a float array, it will take float values?
float f = 23.23f;double, hence you must specify a float if you want to use it as such. Either via a cast as you do, or as a float literal as Scary Wombat suggests. The compiler could assume you meantfloat, but that might not be what you want, hence the warning/error.doubletofloatcan change the value, because thedoublevalue must be rounded to fit in thefloatprecision. So I presume the Java designers do not allow implicitdoubletofloatconversions to avoid this change in value without an explicit request by the programmer. The specific rules for conversions allowed for assignments are here, but I am not familiar enough with the Java specification to interpret it readily and…