Skip to content
Related Articles

Related Articles

Improve Article
Difference between Type Casting and Type Conversion
  • Difficulty Level : Basic
  • Last Updated : 08 Jun, 2020

1. Type Casting:
In typing casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In typing casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that’s why it is also called narrowing conversion.

Syntax/Declaration:-

destination_datatype = (target_datatype)variable;


(): is a casting operator.

target_datatype: is a data type in which we want to convert the source data type.

Type Casting example –

float x;
byte y;
...
...
y=(byte)x;  //Line 5 

In Line 5: you can see that, we are converting float(source) data type into byte(target) data type.



2. Type conversion :
In type conversion, a data type is automatically converted into another data type by a compiler at the compiler time. In type conversion, the destination data type cannot be smaller than the source data type, that’s why it is also called widening conversion. One more important thing is that it can only be applied to compatible data types.

Type Conversion example –

int x=30;
float y;
y=x;  // y==30.000000. 

Let’s see the difference between Type casting and Type conversion which are given below:

S.NOTYPE CASTINGTYPE CONVERSION
1.In type casting, a data type is converted into another data type by a programmer using casting operator.Whereas in type conversion, a data type is converted into another data type by a compiler.
2.Type casting can be applied to compatible data types as well as incompatible data types.Whereas type conversion can only be applied to compatible datatypes.
3.In type casting, casting operator is needed in order to cast the a data type to another data type.Whereas in type conversion, there is no need for a casting operator.
4.In typing casting, the destination data type may be smaller than the source data type, when converting the data type to another data type.Whereas in type conversion, the destination data type can’t be smaller than source data type.
5.Type casting takes place during the program design by programmer.Whereas type conversion is done at the compile time.
6.Type casting is also called narrowing conversion because in this, the destination data type may be smaller than the source data type.Whereas type conversion is also called widening conversion because in this, the destination data type can not be smaller than the source data type.
7.Type casting is often used in coding and competitive programming works.Whereas type conversion is less used in coding and competitive programming as it might cause incorrect answer.
8.Type casting is more efficient and reliable.Whereas type conversion is less efficient and less reliable.

Attention reader! Don’t stop learning now. Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :