Implicit conversions happen at runtime in a number of contexts:
- in assignment statements;
- when values are passed as function arguments;
- when values are returned from functions;
- in as expressions; and
- in expressions using certain operators, such as the addition (+) operator.
For user defined types, implicit conversions succeed when the value to be converted is an instance of either the destination class or a class that derives from the destination class. If an implicit conversion is unsuccessful, an error occurs. For example, the following code contains a successful implicit conversion and an unsuccessful implicit conversion.
class A {}
class B extends A {}
var objA:A = new A();
var objB:B = new B();
var arr:Array = new Array();
objA = objB; // conversion succeeds
objB = arr; // conversion fails
For primitive types, implicit conversions are handled by calling the same internal conversion algorithms that are called by the explicit conversion functions. The following sections discuss these primitive type conversions in detail.