Java – Convert floating point arrays
•
Java
I've been writing Java for some time and even started teaching it to others I find it difficult to explain to new students why floating point array values must be converted 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 guess values containing decimals are only considered doubles, not floating-point numbers, so they need to be projected What is the reason why Java chose it? The compiler can't think that because it is a floating-point array, will it adopt floating-point values?
Solution
Numbers with decimal points in Java are implicitly treated as double For example, you can simply write 23.23f instead of converting to floating point numbers The final f tells the compiler to treat the value as float text
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码