About Question enthuware.ocpjp.v11.2.3048 :
Moderator: admin
-
- Posts: 2
- Joined: Fri Aug 06, 2021 7:46 am
- Contact:
About Question enthuware.ocpjp.v11.2.3048 :
Why 3.0 not 3.0f?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3048 :
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.
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.
Who is online
Users browsing this forum: No registered users and 7 guests