Java: double vs float

后端 未结 6 1183
一整个雨季
一整个雨季 2020-12-09 16:37

In another Bruce Eckel exercise, the code I\'ve written takes a method and changes value in another class. Here is my code:

class Big {
  float b;
}

public         


        
相关标签:
6条回答
  • 2020-12-09 16:42

    Don't use float. There is almost never a good reason to use it and hasn't been for more than a decade. Just use double.

    0 讨论(0)
  • 2020-12-09 16:42

    float range is lower than double so a float can be easily represented in double, but the reverse is not possible because, let say we take a value which is out of float range then during convention we will lose the exact data

    0 讨论(0)
  • 2020-12-09 16:46

    Yes, but you have to specify that they are floats, otherwise they are treated as doubles:

    z.b = 2.2f
    

    The 'f' at the end of the number makes it a float instead of a double.

    Java won't automatically narrow a double to a float.

    0 讨论(0)
  • 2020-12-09 17:01

    No, floats can be automatically upcast to doubles, but doubles can never be floats without explicit casting because doubles have the larger range.

    float range is 1.40129846432481707e-45 to 3.40282346638528860e+38

    double range is 4.94065645841246544e-324d to 1.79769313486231570e+308d

    0 讨论(0)
  • 2020-12-09 17:02
    can't doubles be floats as well?
    

    No. Each value or variable has exactly one type (double, float, int, long, etc...). The Java Language Specification states exactly what happens when you try to assign a value of one type to a variable of another type. Generally, assignments of a "smaller" value to a "larger" type are allowed and done implicitly, but assignments where information could be lost because the target type is too "small" to hold all values of the origin type are not allowed by the compiler, even if the concrete value does fit into the target type.

    That's why the compiler complains that assigning a double value (which the literal implicitly is) to a float variable could lose information, and you have to placate it by either making the value a float, or by casting explicitly.

    One area that often causes confusions is calculations, because these are implicitly "widened" to int for technical reasons. So if you multiply two shorts and try to assign the result to a short, the compiler will complain because the result of the calculation is an int.

    0 讨论(0)
  • 2020-12-09 17:04

    By default, Java will treat a decimal (e.g. "4.3") as a double unless you otherwise specify a float by adding an f after the number (e.g. "4.3f").

    You're having the same problem on both lines. First, the decimal literal is interpreted as a double by the compiler. It then attempts to assign it to b, which is of type float. Since a double is 64 bits and a float is only 32 bits (see Java's primitives documentation), Java gives you an error indicating that the float cannot fit inside the double. The solution is to add an f to your decimal literals.

    If you were trying to do the opposite (i.e. assign a float to a double), that would be okay since you can fit a float's 32 bits within a double's 64.

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