20 Apr 2017

Difference Between Type Casting and Type Conversion


Binding1The two terms “type casting” and “type conversion” occur when there is a need to convert one data type to another. When the two types are compatible with each other, then the conversion of one type to other is done automatically by the compiler. However there is a basic difference between type conversion and type casting i.e. type conversion is done “automatically” by compiler whereas, type casting is to be “explicitly done” by the programmer. Let’s discuss the difference both type casting and conversion with the help of comparison chart.

Content: Type Casting Vs Conversion

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart:

BASIS FOR COMPARISONTYPE CASTINGTYPE CONVERSION
MeaningOne data type is assigned to another by the user, using a cast operator then it is called "Type Casting".Conversion of one data type to another automatically by the compiler is called "Type Conversion".
AppliedType casting can also be applied to two 'incompatible' data types.Type conversion can only be implemented when two data types are 'compatible'.
OperatorFor casting a data type to another, a casting operator '()' is required.No operator required.
Size of Data TypesDestination type can be smaller than source type.Here the destination type must be larger than source type.
ImplementedIt is done during program designing.It is done explicitly while compiling.
Conversion type
Narrowing conversion.Widening conversion.
Exampleint a;
byte b;
...
...
b= (byte) a;
int a=3;
float b;
b=a; // value in b=3.000.

Definition of Type Casting

Type casting can be defined as, casting of one data type to another data type, by the programmer, at the time of program design. Aa automatic conversion of one data type to another, is not possible all the time. It may be the condition that the ‘destination type’ is smaller than the ‘source type’ so, the programmer has to cast explicitly the larger data type to smaller data type using the casting operator'( )’. As the larger data type is modulated to the smaller data type, it is also called ‘narrowing conversion’.
Casting nDeclaration:
  1. destination_type = (target_type) variable/value
  2. //target type is a type in which you want to convert the source type it is always the destination type.
Let’s understand it with an example. You want to convert the data type ‘int’ to ‘byte’, now, as ‘byte’ is smaller than ‘int’, type conversion is not allowed. Here, we had to implicitly convert ‘int’ into ‘byte’ by using casting operator ‘()’. As ‘int’ is larger than ‘byte’, the size of ‘int’ will be reduced to “int mod byte” range.
  1. int a;
  2. byte b;
  3. ...
  4. ...
  5. b=(byte)a;
When the ‘float’ is converted to ‘int’, the size of float get truncated, as ‘int’ does not store the fractional value. If the size of destination type is too small for the source type to fit into, then source type is modulo destination type ‘range’. Casting can also be applied when data types are compatible, it is good practice to use type casting wherever type conversion are needed.

Definition of Type Conversion

Type conversion is the automatic conversion of one data type to another whenever required, done explicitly by the compiler. But there are two conditions to be satisfied before type conversion.
  • Source and destination type must be compatible.
  • Destination type must be larger than source type.
These two conditions should satisfy to achieve type conversion, and this kind of conversion is called ‘widening conversion’, as a smaller type is converted to larger type, widening of type occurs. For this widening conversion, numeric types as ‘int’, ‘float’ are compatible with each other while numeric to char and boolean or char to boolean is also not compatible.Conversion n
This example will provide a better view of this
  1. int a=3;
  2. float b;
  3. b=a; // value in b=3.000.
Here, ‘int’ is converted to ‘float’ which is larger than ‘int’, so widening of source type occurs.  Here, no casting operator is required as the compiler will do it explicitly.

Key Differences Between Type Casting and Conversion

  1. The basic difference which distinguishes type casting from type conversion is that type casting is conversion of one type to another, done by the programmer. On the other hand, the type conversion is conversion of one type to another, done by the compiler while compiling.
  2. Type casting can be applied to the datatypes which may not be compatible with each other but, type conversion can only be applied to the datatypes which are compatible with each other.
  3. The conversion of one type to another in type casting requires the casting operator “( )” while, the conversion of one data type to another in type conversion do not require any operator.
  4. While converting one data type to another in type casting, the destination type can be larger or smaller than the source type. On the other hand, the destination type must be larger than the source type in type conversion.
  5. The conversion of one type to another type is done while coding in type casting whereas, in type conversion, the conversion of one type to another is done explicitly during compilation.
  6. Type casting is called narrowing conversion because here the destination type can be smaller than source type whereas, type conversion is called widening conversion because here, the destination type must be larger than the source type.

Conclusion:

It can be concluded that type conversion and type casting, both perform the task of converting one data type to another but differ in a sense that type casting is done by the programmer, using cast operator'()’ and type conversion is done by the compiler, and it doesn’t use any operator.

No comments:

Post a Comment

commnet here