Type conversion is a technique used in programming to change the data type of an entity. It allows for certain effects that may be difficult to achieve within the confines of a programming language. Type conversion can occur on fundamental types or objects and can be implicit or explicit.
Type conversion is a form of punning, a technique used in computing and computer software programming in which the data type of a specific entity is changed to a different data type. The concept of puns arose to circumvent the specified type system of a programming language so that certain effects could be achieved that would otherwise be difficult to achieve within the confines of the formal definition of the programming language. Type conversion, being the most common form of punning type, is possible in virtually all programming languages, although some languages offer additional punning methods, such as merging or reinterpreting. Some programming style guides suggest not using pun type, even though type conversion is still used frequently.
Type conversion is also sometimes referred to as typecasting or coercion, depending on the programming language used. Every programming language has rules about how to use type conversion successfully. In general, conversions can occur on fundamental types or objects.
There are several fundamental data types or classes of data types. A common data type used in mathematical operations are integers, or simple numbers, which can range from thousands to millions, depending on the number of bits in their width. For example, a 32-bit integer can range from zero to 4,294,967,295 or -2,147,483,648 to 2,147,483,648. Another type is the floating point number, which is pretty much any number with a decimal point.
Single characters, such as a letter of the alphabet or a punctuation mark, can also be used as the data type. A string data type is any uninterrupted group or sequence of numbers and/or letters; string lengths are arbitrary, but their size or limit is usually defined in the programming language. The oldest of the data types is the boolean, which is simply true or false. Other type classes include algebraic, functions, machine data, and objects. For example, in object-oriented languages, an object’s ancestor can use the type of a parent object so that their interactions with each other are more fluid.
The specifics of type conversion don’t really come into play until the rules of a particular programming language are considered. Some language rules also determine whether a type conversion is implicit or explicit. An implicit type conversion is more commonly called coercion and usually takes place when a program is compiled from source code into an executable program. This can happen, for example, if multiple data types are used in an expression and then a comparison is performed. Type conversion is considered implicit, and the compiler handles it automatically; an explicit type conversion is defined in the program code. The C programming language distinguishes 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 that receives a decimal number, a floating-point data type, as input may need to convert the number to an integer data type to perform mathematical operations, or vice versa. This conversion allows the operation to continue.
Protect your devices with Threat Protection by NordVPN