Learn something new every day
More Info... by email
Type conversion is a form of type punning, a technique used in computer science and computer software programming in which the data type of a specific entity is changed into a different data type. The concept of type punning came about to work around the specified type system of a programming language so certain effects can be achieved that would otherwise be difficult to do within the confines of the programming language's formal definition. Type conversion, being the most common form of type punning, is possible in virtually all programming languages, though some languages offer additional methods of type punning, such as union or reinterpretation. Some programming style guides suggest against type punning, even though type conversion is frequently used anyway.
Type conversion is also sometimes referred to as typecasting or coercion, depending on the programming language being used. Each programming language has rules on how type conversion can be used successfully. Generally speaking, conversions can take place on either fundamental types or objects.
There are several fundamental data types or classes of data types. One common data type used in mathematical operations is integers, or plain numbers, that can range into the thousands or millions, depending on the number of bits in their width. For example, a 32-bit integer can range from zero through 4,294,967,295, or -2,147,483,648 through 2,147,483,648. Another type is the floating-point number, which is basically any number with a decimal point.
Single characters, such as a letter of the alphabet or a punctuation mark, can also be used as a data type. A string data type is any group or sequence of numbers and/or letters without a break; string lengths are arbitrary, but their size or limit is usually defined in the programming language. The oldest of data types is the Boolean, which is simply either true or false. Other classes of types include algebraic, functions, machine data, and objects. For example, in object-oriented languages, the ancestor of an object can use the type of a parent object so their interactions with each other go more smoothly.
The specifics of type conversion really don't come into play until a particular programming language's rules are taken into consideration. Some language's rules further dictate whether a type conversion is implicit or explicit. An implicit type conversion is most frequently called a coercion and usually takes place at the time a program is compiled from source code into an executable program. This can happen, say, if multiple data types are used in an expression and then a comparison is executed. The type conversion is considered implied, and the compiler handles it automatically; an explicit type conversion is defined in the code of the program. The C programming language makes a distinction between the two, calling an implicit type conversion a coercion, and an explicit type conversion a cast.
A simple example of a common type conversion would be mathematical operations on numbers in a computer program. A computer program receiving a decimal number, a floating-point data type, as input may need to convert the number into an integer data type to perform mathematical operations, or vice versa. This conversion allows the operation to proceed.