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;
You can explicitly convert a double to an int: int x = (int) 5.0;
Why can't you implicitly convert a double to an int?: int x = 5.0;
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