The procedure of converting one data types into its equivalent anther data type is known as type casting.
500rs ------------- 1 single note; 500 one rupee coins
Type casting divided into two 2 types. These are the
- Implicit type casting
- Explicit type casting
Implicit type casting is storing small values into its equivalent bigger location.
Explicit type casting is storing bigger values into its equivalent small location.
The above two definitions are not always true. There are some contraventions.
Class Demo C
Public static void main (string args [ ])
Int I = 6;
Float j= 7;
System. Out. Prinln (j); // here j = 7.0
Float k = 7.0;
System. Out. Prinln (k); \\ here we get a compilation error.
Float k = (float) 7.0; \\ explicit type casting.
Long l = 8; \\ implicit type casting.
Int x = l;
Byte b =6;
\\Here we don’t get any error. In
implicit type casting takes place. (This type of implicit type casting is supported by only some versions of JVM).
b = x; // error.
b = (byte) x; \\ these case explicit type casting is highly recommended.
Float c = 7 .8 f; \\ this is another method of type casting.
Char ch = (char) x; // this is possible.
System. Out. Prinln (ch);
Char ch1 = ‘&’;
System. Out. Prinln (ch1);
Int z = (int) ch; // this is also possible.
We can type casting certain primitive data types into some certain primitive data types only.
Type casting any primitive data types into any other primitive data types is not possible.