Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between 'Float a = 3f' and 'Float a = 3.0' in java?

If I perform

  97346822*3f, result is 2.9204048E8,

however

 97346822*3.0 gives me 2.92040466E8.

Please explain.

like image 256
ans7991 Avatar asked Jan 12 '23 19:01

ans7991


2 Answers

The number 3.0 is the literal representation of a double value (it's equivalent to 3.0d), whereas 3.0f is a float value. The different precisions explain why you're getting different results - a double is stored using 64-bits, a float uses 32-bits.

like image 155
Óscar López Avatar answered Jan 17 '23 16:01

Óscar López


97346822*3.0

will be treated as double.

Based on oracle tutorial

The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice.

97346822*3f

will be treated as float.

like image 27
kosa Avatar answered Jan 17 '23 15:01

kosa