Floating point literal, floating literal, double Literal

后端 未结 3 1818
清歌不尽
清歌不尽 2020-12-21 01:11

Is the \'floating point literal\' and the \'floating literal\' the same thing in Java?

Also, is there anything called \'double literal\' in Java?

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-21 01:41

    Java has both float literals marked with a f suffix (for example, 2.5f) and double literals, which don't require a suffix (for example 2.5).

    The difference between the two can be demonstrated when you attempt to assign such literals to variables :

    double dl = 2.5; // passes compilation
    float fl1 = 2.5; // error - "Type mismatch: cannot convert from double to float"
    float fl2 = 2.5f; // passes compilation
    

    Both can be considered as floating point literals, since both double and float are floating point types.

提交回复
热议问题