Type Casting
Type Conversion (Type Casting)
When an expression of a given type is converted into another type, then this process is known as type-casting.
Type conversion can be
• Implicit conversion.
• Explicit conversion.
Implicit Conversion
Implicit Conversion are automatically performed when a value is copied to a compatible type, they do not require any operator.
For example:
int a;
float b=5.55555;
a=b;
Here the value of b is converted to integer before being assigned to a. Thus the fractional part of b is truncated.
Explicit Conversion
Many conversions, especially those that imply a different interpretation of the value, require an explicit conversion. Two notations for explicit type conversion are functional and c-like casting.
For example:
short a=5000;
int b;
b = (int) a; // c-like cast notation
b = int (a); // functional notation
Posted in Computer Science, Information Technology, Object Oriented Programming, Object Oriented Programming |
