What do F and D mean at the end of numeric literals?

后端 未结 6 2020
离开以前
离开以前 2020-11-28 13:56

I\'ve seen some of this symbols, but I cannot find anything strange with it,

double d = 5D;
float f = 3.0F;

What does the D and F behind 5

相关标签:
6条回答
  • 2020-11-28 14:35

    D stands for double

    F for float

    you can read up on the basic primitive types of java here

    http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    I would like to point out that writing

    5.1D or 5.1 : if you don't specify a type letter for a comma number then by default it is double

    5 : without the period, by default it is an int

    0 讨论(0)
  • 2020-11-28 14:40

    Means that these numbers are doubles and floats, respectively. Assume you have

    void foo(int x);
    void foo(float x);
    void foo(double x);
    

    and then you call

    foo(5)
    

    the compiler might be stumped. That's why you can say 5, 5f, or 5.0 to specify the type.

    0 讨论(0)
  • 2020-11-28 14:40

    It defines the datatype for the constants 5 and 3.0.

    0 讨论(0)
  • 2020-11-28 14:50

    They're format specifiers for float and double literals. When you write 1.0, it's ambiguous as to whether you intend the literal to be a float or double. By writing 1.0f, you're telling Java that you intend the literal to be a float, while using 1.0d specifies that it should be a double. There's also L, which represents long (e.g., 1L is a long 1, as opposed to an int 1)

    0 讨论(0)
  • 2020-11-28 14:55

    D stands for double and F stands for float. You will occasionally need to add these modifiers, as 5 is considered an integer in this case, and 3.0 is a double.

    0 讨论(0)
  • 2020-11-28 14:56

    As others have mentioned they are the Type definitions, however you will less likely see i or d mentioned as these are the defaults.

    float myfloat = 0.5; 
    

    will error as the 0.5 is a double as default and you cannot autobox down from double to float (64 -> 32 bits) but

    double mydouble = 0.5;
    

    will have no problem

    0 讨论(0)
提交回复
热议问题