Type Casting in java
Implicit conversion :
- The compiler is responsible for Implicit type cast conversion
- Whenever we need to cast a smaller data type value to a bigger data type variable then implicit conversion is used.
- Implicit typecast is also know as widening or upcasting.
- Implicit type casting no loss of information
Example 1:
int a = 'a';
System.out.prinitln(a); // 97
In the above Example compiler converts char to int automatically by implicit typecasting.
Example 2:
double d = 10;
System.out.println(d); // 10.0
In the above Example compiler converts int values to double automatically by implicit typecasting.
Below are possible implicit typecasting :
Explicit conversion :
- The programmer is responsible for Explicit type cast conversion
- Whenever we need to cast a Bigger data type value to a smaller data type variable then Explicit conversion is used.
- Explicit typecast is also know as narrowing or downcasting.
- in the Explicit type casting loss of information
Example 1:
Char a = (char)10;
System.out.prinitln(a);
Below are possible Explicit typecasting :
Comments
Post a Comment