Type Casting
Casting one type to another type is known as type casting there are two types of casting
1. Data type casting : casting one data type to any other data type is known as data type casting
a. Widening data type casting : casting lower size data type to any other higher size data type. It can be implicit and explicit.
Implicit Example : byte b = 10; // 1 byte = 8 bit
Short s = b ; // 2 byte = 16 bit
Explicit data type example: byte b =10;
Short s = (short)b;//
a. Narrowing data type casting : casting higher size data type to anyother lower size data type. It can be done only explicit.
Example Short s = 10;
Byte b = (byte)s;
Q. Why narrowing explicit only ?
There are chances of data loss that is why narrowing is always explicit.
1. Class type casting.
No comments:
Post a Comment