Difference between Type Casting and Type Conversion

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.NO TYPE CASTING TYPE 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. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : pp_pankaj

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.