Float: single-precision floating point number

Double: indicates a double-precision floating point number

The main differences between the two are as follows:

01. Different bytes are occupied in memory

Single-precision floating-point numbers take up 4 bytes in machine memory

A double – precision floating – point number takes up 8 bytes in machine memory

02. Different significant digits

Single-precision floating point significant number 8 bits

A double – precision floating – point number is a valid number of 16 digits

03. Value range

The range of single-precision floating point numbers is -3.40e +38 to 3.40E+38

The range of double – precision floating – point numbers is -1.79e +308 to -1.79e +308

04. Different processing speeds in the program

In general, the CPU can process single-precision floating-point numbers faster than double-precision floating-point numbers

If not declared, the default decimal is double, so if you want to use float, you have to strong-cast it

For example, float a=1.3; Float a = (float)1.3; float a = (float)1.3; Or float a = 1.3f; (either f or f can be case insensitive)

Note: Float is an 8-digit significant number, and the seventh digit will be rounded

Interview questions:

1. What will be returned from 3*0.1==0.3 in Java? True or false?

False, because floating-point numbers cannot be represented exactly and usually lose precision.

2. Float f = 3.4; Is that correct?

Float f = (float)3.4; float f = (float)3.4; Or float f = 3.4f; Can.

Public class Main {public static void Main (String[] arg){system.out.println (1*0.3==0.3); System. The out. Println (3 * 0.1 = = 0.3); System. The out. Println (f = 3 * 0.1 = 0.3 f); }}Copy the code

Output result: