About Question enthuware.ocpjp.v11.2.3048 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
lxfusst
Posts: 2
Joined: Fri Aug 06, 2021 7:46 am
Contact:

About Question enthuware.ocpjp.v11.2.3048 :

Post by lxfusst »

Why 3.0 not 3.0f?

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3048 :

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests