float variables initialization java

前端 未结 6 1101
面向向阳花
面向向阳花 2021-01-23 04:17

The following code snippet gave me compiler error at Line 1.

public abstract class xyz
{

    float Gamma = 20.0; //Line 1
    public class Alpha
    {
        v         


        
6条回答
  •  粉色の甜心
    2021-01-23 04:45

    Per the JLS, §3.10.2, all floating point literals are interepreted as double unless specified as a float.

    A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (§4.2.3).

    Change your declaration to:

    float density = 20.0f;
    

    In general, consider why you're using float - it has less precision than double, and isn't used nearly as often.

提交回复
热议问题