Overview of Type Casting
Typecast is a way of changing an object from one data type to the next. It is used in computer programming to ensure a function handles the variables correctly. A typecast example is the transformation of an integer into a string. This could be used to compare two numbers if one is stored as a string and the other is an integer. If compatible, Java must automatically perform a conversion called Automatic Type Conversion and, if not, it must be specifically cast or converted.
What is Type Casting?
Typecasting may be defined as the process of converting the data type of the outcome of any operation to another data type. This is one of the crucial options provided by the C language to protect the unwanted consumption of memory. It is the way to make the variable store the value of any operation in a manner so that it consumes the limited memory. It is actually introduced to improve the efficiency of memory management. Typecasting makes it very simple to convert the data type of the values but we have to make sure that the values that we are converting should be the right one. For instance, converting the character to integer may sound strange but converting the float value into integer makes sense.
Typecasting can be introduced using the appropriate syntax which has a particular way of defining. Sometimes the type conversion may happen on its own while sometimes we will need to do that. We will be covering the mode of conversion in the next section. There are several things we have to take care of while using type casting like the correct syntax, the correct data type that has to be converted and so on. It helps in developing the program that works smoothly due to very less memory consumption and also helps the program in its fast execution.
Types of Type Casting
Based on how the conversion of the value of any data type is taking place, the typecasting has been divided into two types that are explained below:
1. Implicit Conversion
In this mode of typecasting, the value of one data type could be converted to the other with its own and we will not be required to mention anything. It all happens on its own or automatically. It usually happens when the variable that has to store the converted value has the size more as compared to the value that has to be converted. For instance, when there is a need to convert the integer value to the float value, then it will happen on its own as the size of the integer is 2 bytes while the float is capable to hold a value of 4 bytes.
Float a = 7/6
2. Explicit Conversion
This kind of typecasting, we are supposed to explicitly define the data type in which we want to convert any value. unlike implicit conversion, it has to be mentioned the data type before the operation or value in which it has to be converted. It is used when we have to store the value of any data type that occupies more memory as compared to the variable that is going to store it. For example, the float variable occupies 4 bytes while the integer takes 2 bytes of the memory. In order to store the float value in the integer variable, we will need to mention it before the float value that we want to convert it into an integer. as an outcome, the values existing after a decimal of the float value will vanish and whatever was there before the decimal will be stored in the integer variable.
Int a = (int) 7/6
Understanding Type Casting with an Example
Here we will walk through a simple example to understand how the typecasting works in the real program. So let’s get started!
p = (int) i;
printf("Explicit value is %d",p);
In the above example, we have taken a float variable that will be storing the value 3.54. Being a float variable, it is consuming 4 bytes of memory. Our goal here was to store the float value into the integer variable by using explicit typecasting. In the above example, it is the 7th line that introduces the concept of type casting into the program. The int keyword written right before the i variable has converted the float value of i into an integer value that will eventually lose all the values after the decimal and will only retain the value before the decimal. The outcome of the above code will be 3 which is an integer value and has been converted from the float data type.
Typecasting can be considered as the functionality that lets us convert the data type of any value or variable so that it could be stored in a manner that consumes less memory. It is available in the C language and helps us to make the program light weighted which directly leads to the fast execution of the program. There are several data type conversion that could be done using this feature. It can be perfectly used in any kind of program regardless of its size and complexity.
The program developed with keeping memory management as the primary goal must have to use this option as it the only feature in C language that ensures the type conversion in runtime. In addition to making the program execution a bit fast, it also makes the program looks sophisticated and helps other programs to understand what’s going on with ease. It doesn’t need any expertise to work with typecasting and all one needs to make sure that data type has to be considered while conversion.
This is a guide to What is Type Casting. Here we discuss the types, understanding, and examples of Type Casting along with code. You may also look at the following articles to learn more-