Encyclopedia  |   World Factbook  |   World Flags  |   Reference Tables  |   List of Lists     
   Academic Disciplines  |   Historical Timeline  |   Themed Timelines  |   Biographies  |   How-Tos     
Your Ad Here
Sponsor by The Tattoo Collection


Typecast
Main Page | See live article | Alphabetical index

Typecast

A typecast is both an expression and an action meaning to change from one type or scheme to another. In human languages, it is an expression meaning that someone or something changed to something else, be it a state (such as "typecasting" from rich to poor) or a classification (such as "typecasting" from a caterpillar to a butterfly, also expressed using the word "converted"). In computer programming, it means to convert a variable's data type to another data type, which is only useful for static languages, or languages that require variables to be declared with a data type that limits what can be stored in the declared variable. Programming languages that exhibit such a feature of typecasting are C/C++, Java, Applescript, and many other modern or object-oriented programming languages. The expression used in human language is not very complicated to understand, however, understanding typecasting in programming languages is very difficult at first, as it can cause errors in programs if not done right.

Typecasting in Programming

In computer programming, typecasts are used too often. It's easy to understand why: variables just cannot be used for every type of data, and to convert it into another format is difficult if an obvious built-in cast is not used. But it can also be dangerous because typecasting occurs at runtime, as in the compiler just checks to make sure the syntax is correct, but not that the cast will make perfect sense. Therefore, it is necessary to know what type of data a variable is, and to make sure that its target conversion type will make sense and not cause errors. Some examples of safe conversions are floating-point numbers to integers, characters to integers (returns the character's ASCII value), string types to other string types (such as character arrays to an official string data type), and several others that are not very significant. Some unsafe casts are floating-point values to characters (since ASCII values are all integers), numerical arrays to character arrays and vice-versa, contrasting dimension arrays (such as a two dimensional array into a three dimensional array), and many others. Often times, a language will have a "safe-cast", in which the compiler/interpreter will try harder to guess at a cast's integrity, and may even have runtime checking so that if a cast throws an error, the cast is ignored. C++ has an implementation of several "safe-casts", such as "static_cast<>", "dynamic_cast<>", "reinterpret_cast<>", and "const_cast<>", all with the target type in the brackets and the source variable in parenthisis following the keyword. Most object-oriented programming languages have a convention to cast variables, and a lot of them have "safe-casts".