Automatic type conversion and type casting in Java

Automatic type conversion is possible in Java when source type and destination types are compatible to each other and when size of destination type is bigger than size of source type. For example, an int value can automatically assign to long variable. However an automatic conversion is impossible for a double value to byte variable.

However, when automatic type conversion is not possible then you could do it by explicit type conversion using cast.


(target_type) value

For Example: To convert double to byte:

double d;

byte b;

// …

b = (byte) d;

Leave a Reply