Type Casting
In any application, there may be many situations in which one data type may need to be converted to another data type.
There are two types of conversion: implicit and explicit.
Implicit casting means simply assigning one entity to another without any transformation guidance to the compiler. This type of casting is not permitted in all kinds of transformations and may not workout for all application scenarios.
Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler.
Example: java code
int t = 100;
long h = t; //Implicit casting
For example, the following is legal C language code: for implicit type conversion
double d;
long l;
int i;
if (d > i) d = i;
if (i > l) l = i;
if (d == l) d *= 2;
Although d, l and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded down). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly (for example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to situations such as storing the same integer value into two variables of type int and type single which return false if compared for equality.
Explicit
The most common form of explicit type conversion is known as casting. Explicit type conversion can also be achieved with separately defined conversion routines
Explicit casting means very specifically informing the compiler about the transformation that is expected.
long h = 100.00;
t = (int) h; //Explicit casting
Whenever you are trying to do implicit casting in those scenarios in which you are supposed to use explicit casting, the compiler will throw an exception like this:
Incompatible type for =. Explicit cast needed to convert long to int.
We will try understand these two in greater detail, in the process of casting fundamental data types as well as casting objects which is a bit more tactical.
There are several kinds of explicit conversion.
checked
Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.
unchecked
No check is performed. If the destination type cannot hold the source value, the result is undefined.
bit pattern
The data is not interpreted at all, and its raw bit representation is copied verbatim. This can also be achieved via aliasing.
Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted
// class type-casting
#include
using namespace std;
class CDummy {
float i,j;
};
class CAddition {
int x,y;
public:
CAddition (int a, int b) { x=a; y=b; }
int result() { return x+y;}
};
int main () {
CDummy d;
CAddition * padd;
padd = (CAddition*) &d;
cout << padd->result();
return 0;
}