Representing float values in Java

前端 未结 4 1245
再見小時候
再見小時候 2020-12-03 18:17

Look at the three lines of code below.

  float f = 1;

  float g = 1.1;

  float h = 1.1f;

Second line has compilation errors, while the ot

4条回答
  •  执念已碎
    2020-12-03 18:30

    First line automatically casts int to float (ok).

    Second line could not cast double to float because of loss of precision. You can make an explicit cast:

    float g = (float) 1.1;
    

    Third line does not need modification.

提交回复
热议问题