Normally, the Java compiler will prevent you from assigning primitives that might result in a loss of data. You don't make it clear, but I am assuming that a
is an int
. Assigning a double
to an int
can result in information being lost, so the compiler objects.
If you add a cast the compiler ignores the issue and adds code to do whatever is necessary to convert the value from one type to the value of the other (in this case, assuming a
is an int, simply truncating the double in many cases) and ignores any data loss. The type of the variables remains unchanged. Only the value changes.
There is a bit more to it than that for certain values. For example, if the double has the special value NaN
(not a number) it is converted to int 0
. See in the Java Specification 5.1.3. Narrowing Primitive Conversion.