41. 'float' data type

The variable which is declared using 'float' data type can store decimal values. But the only thing to observe is that the value should end with letter f as shown in the below program.

class Sample
     public static void main(String args[])
          float a=123.456f;   //Observe that the value to be stored in the float declared variable should end with 'f' letter.

Output of this program:


Try this program without adding the letter f at the end of the value. Observe that the program will throw an error on compiling the program.

Please comment below to feedback or ask questions.

How to use the 'char' data type will be explained in the next post.


MS said...

Hello Arun,

Can you please tell me what is the difference between double and float.
As we can save"123.456" using double as well.
So which is more preferable?

Thanks in advance!

Ranjith Kumar said...

The difference is that the float can Hold 6 digits after decimal and double can hold 14 digits after decimal.

float x = 32.33439549594954543593f;
double y = 32.33439549594954543593;



Let me know if that answered your question plz :-)


Arun Motoori said...

@ MS - double is preferable than float.

Arun Motoori said...

@MS - float: 4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

double: 8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

The Java compiler by default treats 123.456 as a double type value. In order to specify that it is a float type value we have to write f at the end of the number i.e. 123.456f.

Shru said...

Thank you Arun! Even i got the same query.

Very useful info.

Ta :)

Sowmya said...

Hi Arun,

For the below code am getting output for"y" as 12345.36528947525 i.e eleven digits after decimal.For double type is it valid?

Could you please let me know if this is correct.

class simple
public static void main(String args[])

{ float var= 111.456567f;
double y= 12345.36528947525123;
System.out.println (var);
System.out.println (y);

Your blog has brought me interest and confidence to learn selenium and Java. :)

Unknown said...

Hi Arun,

As you said float can hold 6 digit after decimat point.i am using below code:-

class FD
public static void main(String args[])
float count=123.12345674f;
double count1=123.123456789123456;

On running code i am getting output 123.12346.My question is should it display 123.123456.Please explain

Arun Motoori said...


I didn't mention that float can hold up to 6 digits. Please find the correct answers below:

Using float, you can store up to 8 digits of value. The digits can be before or after the decimal point.

Examples: 12.345678 or 123456.78

Note: 8 digits is an approximate value, in some cases float only stores 7 digits.

Using double, we can store up to 17 digits of value. The digits can be before or after the decimal point.

Examples: 12.123412341234123 or 1234123412341234.1

Note: 17 digits is an approximate value, in some cases double only stores 16 digits and in some cases double stores 18 digits.

Hope you got my point :)