Pages

Saturday, February 2, 2013

Variable Manipulation (Type Casting) in Java


Variable Manipulation (Type Casting) in Java
Converting one data type into another is called as TypeCasting. Based on conversion it is divided into two types namely "Widening" and "Narrowing"
Widening is defined as storing lower datatype value into a higher data type value. Example storing character (2 bytes) into an integer type (4 bytes).
How is it done ?
char ch="a";
int num=ch;
Narrowing is defined as storing higher datatype value into a lower data type value. Example storing integer (4 bytes) into a character (2 bytes) data type.
How is it done ?
int n=66;
char ch=(char)n;
Example Program for TypeCasting : http://pastie.org/6018640
Casting is an operation and hence it has precedence. The generally allowable order of casting is shown in the diagram below : 

0 comments:

Post a Comment