Why does java make me add f when i use float?

I am very new to programming. I have the following code:

float f = 18.45f; 

this works great. If I changed this to:

 float f = 18.45; 

java says this as an error:

 error: possible loss of precision 

But its optional in terms of double . But in long again I ran into the same problem.

Why does Java make me do this, but not in the case of double ?

+7
source share
1 answer

In Java, 18.45 is a double data type that contains 64-bit. float data type can contain up to 32 bits. Adding extra f makes it float (float literal).

See Primitive data types for more details .

+20
source

All Articles