Java: Why we need to cast a float but not a double?

前端 未结 3 544
别那么骄傲
别那么骄傲 2021-01-17 04:13

I don\'t know if you consider this an important question or not but I want to know. float is a float number (4 bytes). double is a float number (8 bytes). Why we define doub

3条回答
  •  无人共我
    2021-01-17 04:40

    A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

    The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

    double d1 = 123.4;
    // same value as d1, but in scientific notation
    double d2 = 1.234e2;
    float f1  = 123.4f;
    

    See Floating-Point Literals in oracle Java Tutorial

提交回复
热议问题