Operators/Typecasting

From FizzFuzz

Jump to: navigation, search
Go back to Operators.

Typecasting is the conversion of one type to another. This can be coercive, which is implicit typecasting, or casting, which is explicit. This operation is used for casting. This can be conversion from datatype instance to datatype instance, class instance to class instance, or between data type instance and class instance. It is indicated by the ( and ) operators, which are unary and a mix between infix and circumfix operators.

Contents

Notation

([μ]α)β

Where α is a data type or class, μ is a modifier, and β is a literal or a variable. The result of conversion is handled by the class or data type. If undefined for that type, and exception will be throw. Typically, an invalid result will result in either an assignment of null or an exception.

Note

The handling of this varies from data type-to-data type and from class-to-class, and can be highly dependent on the programmer's implementation of a class. Accordingly, the results of this can vary significantly.

Example

int alpha = 0;
int beta = 97;
char gamma = 'a';
 
output << (bool)alpha; // Outputs "false".
output << (char)beta; // Outputs "a".
 
output << (int)gamma; // Outputs "97".
output << (float)gamma; // Outputs "97.0".

See Also