Ada provides a generic library function Unchecked_Conversion.12
Implicit type conversion, also known as coercion or type juggling, is an automatic type conversion by the compiler. Some programming languages allow compilers to provide coercion; others require it.
In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code:
Although d, l, and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when converting representations from floating-point to integer, as the fractional components of the floating-point values will be truncated (rounded toward zero). Conversely, precision can be lost when converting representations from integer to floating-point, since a floating-point type may be unable to exactly represent all possible values of some integer type. For example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can. This can lead to unintuitive behavior, as demonstrated by the following code:
On compilers that implement floats as IEEE single precision, and ints as at least 32 bits, this code will give this peculiar print-out:
Note that 1 represents equality in the last line above. This odd behavior is caused by an implicit conversion of i_value to float when it is compared with f_value. The conversion causes loss of precision, which makes the values equal before the comparison.
Important takeaways:
One special case of implicit type conversion is type promotion, where an object is automatically converted into another data type representing a superset of the original type. Promotions are commonly used with types smaller than the native type of the target platform's arithmetic logic unit (ALU), before arithmetic and logical operations, to make such operations possible, or more efficient if the ALU can work with more than one type. C and C++ perform such promotion for objects of Boolean, character, wide character, enumeration, and short integer types which are promoted to int, and for objects of type float, which are promoted to double. Unlike some other type conversions, promotions never lose precision or modify the value stored in the object.
In Java:
Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is requested by the user in the program.
There are several kinds of explicit conversion.
In object-oriented programming languages, objects can also be downcast : a reference of a base class is cast to one of its derived classes.
In C#, type conversion can be made in a safe or unsafe (i.e., C-like) manner, the former called checked type cast.13
In C++ a similar effect can be achieved using C++-style cast syntax.
In Eiffel the notion of type conversion is integrated into the rules of the type system. The Assignment Rule says that an assignment, such as:
is valid if and only if the type of its source expression, y in this case, is compatible with the type of its target entity, x in this case. In this rule, compatible with means that the type of the source expression either conforms to or converts to that of the target. Conformance of types is defined by the familiar rules for polymorphism in object-oriented programming. For example, in the assignment above, the type of y conforms to the type of x if the class upon which y is based is a descendant of that upon which x is based.
The actions of type conversion in Eiffel, specifically converts to and converts from are defined as:
A type based on a class CU converts to a type T based on a class CT (and T converts from U) if either CT has a conversion procedure using U as a conversion type, or CU has a conversion query listing T as a conversion type
A type based on a class CU converts to a type T based on a class CT (and T converts from U) if either
Eiffel is a fully compliant language for Microsoft .NET Framework. Before development of .NET, Eiffel already had extensive class libraries. Using the .NET type libraries, particularly with commonly used types such as strings, poses a conversion problem. Existing Eiffel software uses the string classes (such as STRING_8) from the Eiffel libraries, but Eiffel software written for .NET must use the .NET string class (System.String) in many cases, for example when calling .NET methods which expect items of the .NET type to be passed as arguments. So, the conversion of these types back and forth needs to be as seamless as possible.
In the code above, two strings are declared, one of each different type (SYSTEM_STRING is the Eiffel compliant alias for System.String). Because System.String does not conform to STRING_8, then the assignment above is valid only if System.String converts to STRING_8.
The Eiffel class STRING_8 has a conversion procedure make_from_cil for objects of type System.String. Conversion procedures are also always designated as creation procedures (similar to constructors). The following is an excerpt from the STRING_8 class:
The presence of the conversion procedure makes the assignment:
semantically equivalent to:
in which my_string is constructed as a new object of type STRING_8 with content equivalent to that of my_system_string.
To handle an assignment with original source and target reversed:
the class STRING_8 also contains a conversion query to_cil which will produce a System.String from an instance of STRING_8.
The assignment:
then, becomes equivalent to:
In Eiffel, the setup for type conversion is included in the class code, but then appears to happen as automatically as explicit type conversion in client code. The includes not just assignments but other types of attachments as well, such as argument (parameter) substitution.
Rust provides no implicit type conversion (coercion) between primitive types. But, explicit type conversion (casting) can be performed using the as keyword.14
A related concept in static type systems is called type assertion, which instruct the compiler to treat the expression of a certain type, disregarding its own inference. Type assertion may be safe (a runtime check is performed) or unsafe. A type assertion does not convert the value from a data type to another.
In TypeScript, a type assertion is done by using the as keyword:15
In the above example, document.getElementById is declared to return an HTMLElement, but you know that it always return an HTMLCanvasElement, which is a subtype of HTMLElement, in this case. If it is not the case, subsequent code which relies on the behaviour of HTMLCanvasElement will not perform correctly, as in Typescript there is no runtime checking for type assertions.
In Typescript, there is no general way to check if a value is of a certain type at runtime, as there is no runtime type support. However, it is possible to write a user-defined function which the user tells the compiler if a value is of a certain type of not. Such a function is called type guard, and is declared with a return type of x is Type, where x is a parameter or this, in place of boolean.
This allows unsafe type assertions to be contained in the checker function instead of littered around the codebase.
In Go, a type assertion can be used to access a concrete type value from an interface value. It is a safe assertion that it will panic (in the case of one return value), or return a zero value (if two return values are used), if the value is not of that concrete type.16
This type assertions tell the system that i is of type T. If it isn't, it panics.
Many programming languages support union types which can hold a value of multiple types. Untagged unions are provided in some languages with loose type-checking, such as C and PL/I, but also in the original Pascal. These can be used to interpret the bit pattern of one type as a value of another type.
In hacking, typecasting is the misuse of type conversion to temporarily change a variable's data type from how it was originally defined.17 This provides opportunities for hackers since in type conversion after a variable is "typecast" to become a different data type, the compiler will treat that hacked variable as the new data type for that specific operation.18
Mehrotra, Dheeraj (2008). S. Chand's Computer Science. S. Chand. pp. 81–83. ISBN 978-8121929844. 978-8121929844 ↩
Programming Languages - Design and Constructs. Laxmi Publications. 2013. p. 35. ISBN 978-9381159415. 978-9381159415 ↩
Reilly, Edwin (2004). Concise Encyclopedia of Computer Science. John Wiley & Sons. pp. 82, 110. ISBN 0470090952. 0470090952 ↩
Fenton, Steve (2017). Pro TypeScript: Application-Scale JavaScript Development. Apress. pp. xxiii. ISBN 978-1484232491. 978-1484232491 ↩
"Type Juggling". PHP Manual. Retrieved 27 January 2019. http://php.net/manual/en/language.types.type-juggling.php ↩
Olsson, Mikael (2013). C++ Quick Syntax Reference. Apress. pp. 87–89. ISBN 978-1430262770. 978-1430262770 ↩
Kruse, Rudolf; Borgelt, Christian; Braune, Christian; Mostaghim, Sanaz; Steinbrecher, Matthias (16 September 2016). Computational Intelligence: A Methodological Introduction. Springer. p. 269. ISBN 978-1447172963. 978-1447172963 ↩
"Unchecked Type Conversions". Ada Information Clearinghouse. Retrieved 11 March 2023. https://www.adaic.org/resources/add_content/standards/95lrm/ARM_HTML/RM-13-9.html ↩
Mössenböck, Hanspeter (25 March 2002). "Advanced C#: Checked Type Casts" (PDF). Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik. p. 5. Retrieved 4 August 2011. at C# Tutorial http://ssw.jku.at/Teaching/Lectures/CSharp/Tutorial/Part2.pdf ↩
"Casting". Rust by Example. Retrieved 1 April 2025. https://doc.rust-lang.org/rust-by-example/types/cast.html ↩
"Everyday Types". The TypeScript Handbook. Retrieved 1 April 2025. https://www.typescriptlang.org/docs/handbook/2/everyday-types.html ↩
"Type assertions". A Tour of Go. Retrieved 1 April 2025. https://go.dev/tour/methods/15 ↩
Erickson, Jon (2008). Hacking: The Art of Exploitation. No Starch Press. p. 51. ISBN 978-1-59327-144-2. "Typecasting is simply a way to temporarily change a variable's data type, despite how it was originally defined. When a variable is typecast into a different type, the compiler is basically told to treat that variable as if it were the new data type, but only for that operation. The syntax for typecasting is as follows: (typecast_data_type) variable ..." 978-1-59327-144-2 ↩
Gopal, Arpita (2009). Magnifying C. PHI Learning Private Limited. p. 59. ISBN 978-81-203-3861-6. "From the above, it is clear that the usage of typecasting is to make a variable of one type, act like another type for one single operation. So by using this ability of typecasting it is possible for create ASCII characters by typecasting integer to its ..." 978-81-203-3861-6 ↩