Convert a value from one data type to another data type is known as type casting. Type casting is when a value of one primitive data type is assigned to another data type.
There are two types of type casting:
- Widening Type Casting
- Narrowing Type Casting
1.
Widening Type Casting
Converting a lower data type into a higher one is
called widening type casting. It is also known as implicit
conversion or casting down. It is done automatically.
byte -> short -> char -> int -> long -> float -> double
This is done when
- Both data types must be compatible with each other.
- The target type must be larger than the source type.
2.
Narrowing Type Casting
Converting a higher data type into a lower one is
called narrowing type casting. It is also known as explicit
conversion or casting up. It is done manually by the programmer.
If casting is not done then the compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte
No comments:
Post a Comment