What is casting? What are the different types of Casting?
Type casting: Explicit conversion between incompatible types ( or ) changing entities from one data type to another data type.
Example:byte number;
number = 14; // error – 14 is int type and number is of byte
number = (byte)14; // correct type casting
Types of casting :1. Implicit casting: This is the process of assigning one entity to another. No transformation guidance is needed.
Example:int number = 1000;
long longNumber = number; //Implicit casting
Because the target type (long) has all values in the range that are available in the source type (int)
2. Explicit casting: This is the process of assigning one entity to another by providing the transformation guidance. The compiler is specifically informed about the data transformation.
Example:long longNumber = 700;
int number = (int) longNumber; //Explicit casting
Because the target type (int) does not have all the values in the range that are available in the source (long)