Differentiate between implicit and explicit types of conversion with
suitable examples..
Answers
Answer:1.Implicit conversion is used to convert(or store) a value of lower data type into a larger datatype,
2. User intervision is not necessary,
3.Does not result in loss of data,
Whereas,
1.Explicit conversion is used to conver a value of higher datatype into a lower datatype value,
2.User intervision is necessary
3. May result in the loss of data.
Explanation:IMPLICIT:
char c='a';
int n=c;
EXPLICIT:
double d=5.56;
int m=(int)d;
Answer:
Implicit casting doesn't require a casting operator. This casting is normally used when converting data from smaller integral types to larger or derived types to the base type.
int x = 123;
double y = x;
In the above statement, the conversion of data from int to double is done implicitly, in other words programmer don't need to specify any type operators.
For example, the values of ushort and char are effectively interchangeable, because both store a number between 0 and 65535. You can convert values between these types implicitly.
There are many implicit conversions of simple types; bool and string have no implicit conversions, but the numeric types have a few. For reference, the following table shows the numeric conversions that the compiler can perform implicitly (remember that chars are stored as numbers, so char counts as a numeric type).
TYPE CAN SAFELY BE CONVERTED TO
byte short, ushort, int, uint, long, ulong, float, double, decimal
sbyte short, int, long, float, double, decimal
short int, long, float, double, decimal
ushort int, uint, long, ulong, float, double, decimal
int long, float, double, decimal
uint long, ulong, float, double, decimal
long float, double, decimal
ulong float, double, decimal
float double
char ushort, int, uint, long, ulong, float, double, decimal
Explicit conversion:
Explicit casting requires a casting operator. This casting is normally used when converting a double to int or a base type to a derived type.
double y = 123;
int x = (int)y;
In the above statement, we have to specify the type operator (int) when converting from double to int else the compiler will throw an error.
Explanation: