Why can't I implicitly convert a double to an int?

前端 未结 5 1599
暗喜
暗喜 2021-01-19 14:20

You can implicitly convert an int to a double: double x = 5;

You can explicitly convert an int to a double: double x = (double) 5;

<
5条回答
  •  情歌与酒
    2021-01-19 14:52

    The range of double is wider than int. That's why you need explicit cast. Because of the same reason you can't implicitly cast from long to int:

    long l = 234;
    int x = l; // error
    

提交回复
热议问题