Got it

Java Float vs Double

Created: Nov 15, 2021 08:44:05Latest reply: Dec 17, 2021 09:00:14 278 4 1 0 0
  Rewarded HiCoins: 0 (problem resolved)

Dear dev team,

What's the difference between Java float and double? Thanks.

Featured Answers

Best answer

Recommended answer

little_fish
Admin Created Nov 15, 2021 08:45:58

Dear Phany,

The float data type is a single-precision 32-bit IEEE 754 floating-point. The double data type is a double-precision 64-bit IEEE 754 floating-point. 

A float is mainly used to save memory in large arrays of floating-point numbers. Always we need to remember that data type should never be used for precise values, such as currency.

The double data type is normally the default choice for decimal values. The data type should never be used for precise values, such as currency.

Default Values:

Data Type

Default Value (for fields)

float

0.0f

double

0.0d

Float data type in Java stores a decimal value with 6-7 total digits of precision. So, for example, 12.12345 can be saved as a float, but 12.123456789 cannot be saved as afloat. When representing a float data type in Java, we should append the letter f to the end of the data type; otherwise, it will save as double.

Double data type stores decimal values with 15-16 digits of precision. The default value is 0.0d; this means that if you do not append for d to the end of the decimal, the value will be stored as a double in Java.

Thanks.

View more
  • x
  • convention:

All Answers

Dear Phany,

The float data type is a single-precision 32-bit IEEE 754 floating-point. The double data type is a double-precision 64-bit IEEE 754 floating-point. 

A float is mainly used to save memory in large arrays of floating-point numbers. Always we need to remember that data type should never be used for precise values, such as currency.

The double data type is normally the default choice for decimal values. The data type should never be used for precise values, such as currency.

Default Values:

Data Type

Default Value (for fields)

float

0.0f

double

0.0d

Float data type in Java stores a decimal value with 6-7 total digits of precision. So, for example, 12.12345 can be saved as a float, but 12.123456789 cannot be saved as afloat. When representing a float data type in Java, we should append the letter f to the end of the data type; otherwise, it will save as double.

Double data type stores decimal values with 15-16 digits of precision. The default value is 0.0d; this means that if you do not append for d to the end of the decimal, the value will be stored as a double in Java.

Thanks.

View more
  • x
  • convention:

Dear Axe,

This infographic can help you understand Java Float vs Java Double:

Java Float vs Java Double

Thanks.

View more
  • x
  • convention:

Though both Java float and Double may represent floating-point figures, there are a few factors in play when selecting between the two. Though both Java float and Double are approximate types, if you require more precise and accurate results, use double. If you have a memory constraint, use float instead of double because it takes up half the space. If your numbers don't fit in the float range, use double instead. So you should exercise caution in coping with floating-point numbers, don't use double or float for monetary figures; instead, use Big Decimal. In Java, both float and double are used to represent real numbers, such as fractions or decimal points. 

JAVA Course in Pune



View more
  • x
  • convention:

little_fish
little_fish Created Jan 10, 2022 15:02:14 (0) (0)
 

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.