double num = 5;
When you do this, it avoids the casting. For more information, you can check the casting conventions here: https://docs.oracle.com/javase/specs/jls/se7/html/index.html
According to convention 5.2:
Widening primitive conversions do not lose information about the overall magnitude of a numeric value.
[...]
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
Thus you can express 5 exactly as a double.