sriram
sriram

Reputation: 9032

Why does java force me to add f when I use float?

I'm very new to programming. I have the following code:

float f = 18.45f;

this works fine. If I change that to:

float f = 18.45;

java saying this as error:

error: possible loss of precision

But its optional in terms of double. But in long again I'm facing the same problem.

Why does java forces me to do so, but not in case with double?

Upvotes: 7

Views: 1973

Answers (1)

Eng.Fouad
Eng.Fouad

Reputation: 117587

In Java, 18.45 is a double data type which holds 64-bit. float data type can hold up to 32-bit only. Adding the extra f makes it a float (float literal).

See Primitive Data Types for more details.

Upvotes: 20

Related Questions