The basic difference between type conversion and type casting, i.e. type conversion is made “automatically” by compiler whereas, type casting is to be “explicitly done” by the programmer.
The 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. Let’s discuss the difference both type casting and conversion with the help of comparison chart.
Content: Type Casting Vs Type Conversion
|Basis for Comparison||Type Casting||Type Conversion|
|Meaning||One 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".|
|Applied||Type casting can also be applied to two 'incompatible' data types.||Type conversion can only be implemented when two data types are 'compatible'.|
|Operator||For casting a data type to another, a casting operator '()' is required.||No operator required.|
|Size of Data Types||Destination type can be smaller than source type.||Here the destination type must be larger than source type.|
|Implemented||It is done during program designing.||It is done explicitly while compiling.|
|Conversion type||Narrowing conversion.||Widening conversion.|
b= (byte) a;
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. 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’. Therefore, the programmer has to cast the larger data type explicitly 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’.Declaration
destination_type = (target_type) variable/value //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.
int a; byte b; ... ... b=(byte)a;
When the ‘float’ is converted to ‘int’, the size of float gets truncated, as ‘int’ does not store the fractional value. If the size of the 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 is 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.
This example will provide a better view of this
int a=3; float b; 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 Type Conversion
- The basic difference which distinguishes type casting from type conversion is that type casting is the conversion of one type to another, done by the programmer. On the other hand, the type conversion is the conversion of one type to another, done by the compiler while compiling.
- Type casting can be applied to the datatypes, which may not be compatible with each other. Conversely, type conversion can only be applied to the datatypes which are compatible with each other.
- 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 does not require any operator.
- While converting one data type to another in type casting, the destination type can be larger or smaller than the source type. As against, the destination type must be larger than the source type in type conversion.
- The conversion of one type to another type is done while coding in type casting. In contrast, in type conversion, the conversion of one type to another is done explicitly during compilation.
- Type casting is called narrowing conversion because here the destination type can be smaller than source type. Unlike, type conversion is called widening conversion because here, the destination type must be larger than the source type.
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.