Page 1 of 1
About Question enthuware.ocpjp.v11.2.3048 :
Posted: Fri Aug 06, 2021 7:48 am
by lxfusst
Why 3.0 not 3.0f?
Re: About Question enthuware.ocpjp.v11.2.3048 :
Posted: Fri Aug 06, 2021 8:12 am
by admin
That is just how the API for
Float.toString is coded. (This is the method that is used to generate the String to be printed when System.out.println(float) is invoked.
The logical explanation is that 3.0f is actually just a format that is used to type a float value in Java code. The compiler requires the suffix 'f' to make sure the programmer is not unwittingly trying to assign a double value (e.g. 3.0) to a float variable.
But in the JVM, there is no difference between 3.0 and 3.0f. The difference is in the sizes of a float and a double variables, not in the values. So, while printing the value, the suffix 'f' is not printed.