Type casting in Java is the process of converting a variable or value from one data type into another.
For primitive data types, Java handles this in two ways: widening (automatic) and narrowing (manual).
Widening converts a smaller data type into a larger data type. Java does this automatically because there is no risk of losing information. Conversion order:
byte→short→char→int→long→float→doubleNarrowing converts a larger data type into a smaller data type. Because data can be truncated or overflow, Java forces you to do this manually by placing the target type in parentheses
()before the value.Conversion order:
double→float→long→int→char→short→byte
int a = 10;
double b = a; // Widening
double x = 10.5;
int y = (int) x; // Narrowing