> Why can't I write "float x = 3.7" in java?

Why can't I write "float x = 3.7" in java?

Posted at: 2014-12-18 
Isn't float same as double? I'm confused...

There is a difference between float and a double.

A double data type is a double-precision 64-bit IEEE 754 floating point.

A float data type is a single-precision 32-bit IEEE 754 floating point.

If you compile the above you will get an error:

error: possible loss of precision

required: float

found: double

A double is generally used as the default data type for decimal values.

Try casting it:

float f = (float)3.7;

Yes and no.

Both are used to represent numbers that have a decimal in them. But "float" is only 32 bits in length so it is limited in how large of a number it can hold. Seven digits (counting on both sides of the decimal) is the limit.

Double is a "float" that is 64 bits longer and can hold much larger numbers.

The statement you have should work. What type of an error is it giving you? Without knowing the error it is difficult to guess why it is failing.

Isn't float same as double? I'm confused...