What Is Type Conversion And Type Casting?

What Is Type Conversion And Type Casting?
What Is Type Conversion And Type Casting?

Introduction

Often, there might be instances where the variable’s data type has to be converted to some other data type in programming. For example, a situation might arise where an integer-type variable has to be converted to a floating-point or vice-versa. In such cases, the data types are converted using concepts like type conversion and type casting.

In this blog, we will briefly cover type conversion and type casting and see the difference between them in detail.

Type Conversion

Type conversion is a process in which the data type is automatically converted into another data type. The compiler does this automatic conversion at compile time. The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type.

For type conversion to take place, the following two conditions must be followed:-

  • The destination data type and source data type must be compatible. For example, numeric types (like ‘int’ and ‘float’) are compatible, while numeric to char / numeric to boolean / char to boolean is not compatible.
  • The destination data type must be larger than the source data type. For example, int can be converted to float, but float cannot be converted to int.

It is also called widening conversion as the source data type with a smaller size is converted into the destination data type with a larger size. In this case, as the lower data types with smaller sizes are converted into higher ones with a larger size, there is no chance of data loss. 

In type conversion, the source data type with a smaller size is converted into the destination data type with a larger size.

Let’s look at an example of type conversion in C++.

#include <iostream>
using namespace std;
int main() {
int intType = 20;
float floatType;
// float(destination data type) is of higher data size than int(source data type) so type conversion occurs
floatType = intType;
 
cout<<floatType<<endl;
return 0;
}

Output:

20

Also, refer to type conversion in Java and Python.

Type Casting

Type casting is a process in which the programmer manually converts one data type into another data type. For this, the casting operator (), the parenthesis, is used. Unlike type conversion, the source data type must be larger than the destination type in type casting.

blog banner 1

In type casting, the source data type with a larger size is converted into the destination data type with a smaller size.

It is also called narrowing conversion as the source data type with a larger size is converted into the destination data type with a smaller size.

In this case, as the higher data types with a larger size are converted into lower ones with a smaller size, there is a chance of data loss. This is the reason that this type of conversion does not happen automatically.

Let’s look at an example of type casting in C++.

#include <iostream>
using namespace std;
int main() {
float floatType = 20.8;
int intType;
// float(source data type) is of higher data size than int(destination data type) so type casting occurs
intType = (int)floatType;
cout<<intType<<endl;
return 0;
}

Output:

20

Also, refer to type casting in Java and Python.

Type Conversion Vs Type Casting

Let’s now see the difference between Type Conversion and Type Casting.

Type ConversionType Casting
The compiler automatically converts a data type into another data type.The programmer converts a data type into another data type.
Applicable only to compatible data types.Applicable to compatible data types as well as incompatible data types. 
No casting operator is required.Casting operator () is used to cast a data type to another data type.
The source data type with a smaller size is converted into the destination data type with a larger size.The source data type with a larger size is converted into the destination data type with a smaller size.
It takes place during the compile time.It takes place when the programmer writes the code.
No chance of data loss.The chance of data loss is high.
Less efficient and reliable as information like signs can be lost (when a signed type is implicitly converted to an unsigned type), or overflow can happen (when long long is implicitly converted to float).More efficient and reliable.
Also called Widening Type Conversion/ Implicit Conversion/ Casting Down.Also called narrowing conversion/ Explicit Conversion/ Casting Up.
Example:int i = 20;float f = i;Example:float f = 10.0;int i = (int) f;

Frequently Asked Questions

What is meant by type conversion and type casting?

In type conversion, a compiler converts a lower data type into a higher data type. In typecasting, a higher data type is converted into a lower data type by the programmer.

What is explicit type conversion?

Explicit type conversion is when a higher data type is converted into a lower data type by the programmer explicitly.

What is automatic type conversion?

Automatic type conversion is when a compiler automatically converts a lower data type into a higher data type.

Differentiate between type conversion and type casting based on data loss.

There is a chance of data loss in type casting while the data is safe in type conversion.

How does data conversion take place in type casting in Python?

In explicit type casting, the data types are converted manually by the programmer using in-built functions.

Key Takeaways

That was all about Type Conversion and Type Casting. To know more about it in Java and in Python to learn the implementation of Type Conversion and Type Casting in the respective languages.

Now that you know what these two major concepts are, you can go ahead and attempt some questions based on them in C++, Java, and Python languages on our CodeStudio Platform!

We hope you found this blog useful. Feel free to let us know your thoughts in the comments section.

By Hari Sapna Nair