Type casting or type conversion involves conversion of one primitive type value to another primitive type either implicitly or explicitly. We can categories Java type conversion in two ways:
Conversion of narrow (smaller) data type value to wider (larger) data type is called widening primitive conversion which happens impicitly. For example byte can be converted to int type easily without lossing data.
Conversion of wider data type to narrower data type is called narrowing primitive conversion which requires explicitly type conversion. For example, converting float to int type will not convert implicitly and require some explicit casting.
Note: While doing explicit type casting, we may loss some amount of actual data.
As we discussed above, Implicit type casting happens automatically by the Java compiler. In the below example, we are casting one smaller type to larger type easily and getting the same output without lossing actual data.
To deal with explicit type casting, Java mandates to perform casting by using the below programming syntax.
In the below example, we are using casting syntax to perform explicit type casting. Basically we are converting larger data type value to smaller data type.
See, when we cast float type value to long type then it truncates decimal point value. that's why we say always be careful while doing explicit type casting in your programs.