101

I don't know why f or F is placed after float values in Java or other languages? for instance,

float fVariable = 12.3f;

any features other than indicating that this is a float value?

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
ipkiss
  • 13,311
  • 33
  • 88
  • 123
  • Same Question: [Java: Why do you need to specify a 'f" in a float literal?](http://stackoverflow.com/questions/14102955/java-why-do-you-need-to-specify-a-f-in-a-float-literal) – Grijesh Chauhan Jul 06 '13 at 16:39
  • Because otherwise they aren't float values, they are ints or doubles. – user207421 Mar 09 '19 at 21:11
  • 1
    @DeepakRaghuwanshi It is more than a little immodest to describe your own blog as 'the best ever explanation for this'. The fact that it is actually a load of rubbish doesn't improve matters. – user207421 Mar 09 '19 at 21:28

9 Answers9

85

By default 12.3 is double literal. To tell compiler to treat it as float explicitly -> use f or F.

See tutorial page on the primitive types.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    Likewise every whole literal is by default int. So when we assign a literal this way: long x = 2147483648; (this is 2^31 > int range but Compiler still consider this as int literal. so we need to tell compiler that this is long by adding L or l ) long x = 2147483648L; – Navdeep Singh Oct 06 '18 at 20:11
  • I might be late to this post, but how come Java can't convert it to float implicitly? We said the type is float, so why would it treat it as default-double when we just told it what it is. – Stefan Oct 08 '20 at 22:36
  • @Stefan I have same question. – Keshav Garg Jun 16 '21 at 13:24
56

Seeing as there are only so many ways to represent a number in your program, the designers of Java had to cherry pick and assign each form to the most common use case. For those forms selected as default, the suffix that denotes the exact type is optional.

  • For Integer literals (int, long), the default is int. For obvious reasons.
  • For Floating point literals (float, double) the default is double. Because using double potentially allows safer arithmetic on the stored values.

So, when you type 12 in your program, thats an int literal, as opposed to 12L, which is a long. And when you type 12.3, thats a double literal, as opposed to 12.3F, which is a float.

So where is this relevant? Primarily in handling downcasts, or narrowing conversions. Whenever you downcast a long to an int, or a double to a float, the possibility for data loss exists. So, the compiler will force you to indicate that you really want to perform the narrowing conversion, by signaling a compile error for something like this:

float f = 12.3;

Because 12.3 represents a double, you have to explicitly cast it to a float (basically signing off on the narrowing conversion). Otherwise, you could indicate that the number is really a float, by using the correct suffix;

float f = 12.3f;

So too summarize, having to specify a suffix for longs and floats is a compromise the language designers chose, in order to balance the need to specify what exactly a number is, with the flexibility of converting numbers from one storage type to another.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • 1
    It's worth noting that if `d` is the best `double` representation of some numerical quantity `x`, `float f=(float)d` will be the closest `float` to some value that's within a part per trillion of `x`, even though it requires a typecast. By contrast, if `f` is the best `float` representation of some numerical value `x`, `double d=f` won't require a typecast even though it will often be nowhere close to the best `double` representation of `x`, and may be off by *hundreds of orders of magnitude*. I find it ironic that `float f=(float)(1.0/10.0)` needs a cast, but `double d=1.0f/10.0f` doesn't. – supercat Jun 10 '13 at 17:33
  • " So, the compiler will force you to indicate that you really want to perform the narrowing conversion, by signaling a compile error for something like this:" Should that also apply to long though – RetroCode Nov 22 '16 at 16:13
  • 1
    @Perception why do you write For obvious reason in first point can you please elaborate it ? – Rishabh Agrawal Feb 21 '17 at 16:08
  • Like [Agrawal](https://stackoverflow.com/questions/9748160/why-f-is-placed-after-float-values#comment71894276_9749119), I too wonder what your “obvious reasons” are. Why is using a 32-bit type for integers by default “obvious” when using a 64-bit type for floating-point by default is “safer”? By that logic, the obvious default for integers would be 64-bit `long`. – Basil Bourque Jul 17 '22 at 01:07
28

float and double can only provide approximate representation values for some values. e.g. 12.3 or 0.1

The difference is that float is not as accurate (as it has less precision, because its smaller)

e.g.

System.out.println("0.1f == 0.1 is " + (0.1f == 0.1));
System.out.println("0.1f is actually " + new BigDecimal(0.1f));
System.out.println("0.1 is actually " + new BigDecimal(0.1));

prints

0.1f == 0.1 is false
0.1f is actually 0.100000001490116119384765625
0.1 is actually 0.1000000000000000055511151231257827021181583404541015625

So 0.1 is the closest representation in double and 0.1f is the closest representation in float

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
6

float fVariable = 12.3; is fine. but when you use only float value(without any identifier) in any expression that time you need to tell compiler that value is float hence we use suffix "f" after value. example

float fl =13f/5f;

here 13 and 5 are float values.

kundan bora
  • 3,821
  • 2
  • 20
  • 29
  • float fVariable = 12.3; is not correct. you always need to add f prefix for float assignment. by default every real number is double. – Navdeep Singh Oct 06 '18 at 20:15
4

In java we have many different basic variable types so in order to make it general , it has some default features. Like if we give an input 16.02 it automatically takes it as a double input. So if you want to specify it as float we mention that 'f' after the number or we can simply use:

float f = (float) 16.02;

or

float f = 16.02f;

In the same way we have to mention 16l if we want the number to be saved as a long type else it will automatically select the default type ie int type.

3

During compilation, all floating point numbers (numbers with decimal point) default to double.

Therefore, if you don't want your number to double and just want it as float, you have to explicitly tell the compiler by adding a f or F at end of the literal constant.

Confuse
  • 5,646
  • 7
  • 36
  • 58
2

If you do not use f it will be interpreted as double, which is the default in Java. You can also write it like this##

float f=(float) 32.5956; float f=32.5956f;

Hasnaa Ibraheem
  • 1,132
  • 1
  • 10
  • 18
Chetan Raj
  • 21
  • 2
  • It is good to have you in stackoverflow. Your answer is okay however other colleagues already gave the same information. A contribution is really valued if it wasn't mentioned before. Welcome on board.. – Hasnaa Ibraheem Nov 03 '19 at 18:37
1

Float is single-precision 32-bit IEEE 754 floating point and Double is double-precision 64-bit IEEE 754 floating point. When you use a value with decimal points and if you don`t specify is as 0.23f (specifically float) java identifies it as a double.

For decimal values, double data type is generally the default choice taken by java. Check This

User123456
  • 2,492
  • 3
  • 30
  • 44
0

[10 years after the original post]

why f or F is placed after float values

With the f, the initialization occurs with the closest float value. Without the f, it might differ.

Many code values like 12.3 are not exactly representable as a double or a float. Instead a nearby value is used.

One rounding

Code 12.3 converted to the closest float: 12.30000019073486328125

float fVariable1 = 12.3f;

Two roundings

Code 12.3 converted to the closest double 12.300000000000000710542735760100185871124267578125 and then to the nearest float: 12.30000019073486328125.

float fVariable2 = 12.3;  

Sometimes that 2-step approach makes for a different value due to double rounding.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256