assign int to byte vs double to float in java

后端 未结 3 2008
粉色の甜心
粉色の甜心 2020-12-16 07:15

1.when we assign double to float variable compiler gives us error

float f = 2753.2211;

possible loss of precision<

3条回答
  •  自闭症患者
    2020-12-16 08:12

    Data isn't going to be lost in the second case.

    A byte comprises the values of -128 and 127 inclusive, so as long as your int fits within that range, no loss of precision can occur.

    Your second value is not a float-literal; by default, all floating point values in Java are double. You can explicitly make that a float by adding f to the end of it.

    float f = 2573.2211f;
    

提交回复
热议问题