Type conversion is a technique in wich variables of different types can be assigned to each other. There are two types of type conversions implicit and explicit. Implicit conversions are handled automatically by the compiler or runtime engine and explicit conversions are handled by the developer.
The following table describes how the conversion of basic scalars works:
| Source | Destination | Conversion method | Conversion test | Conversion errors | How the conversion is done |
|---|---|---|---|---|---|
int | int | - | - | - | |
int | decimal | automatic | - | - | |
int | datetime | - | - | - | |
int | string | automatic | - | - | 10 => "10" |
int | boolean | automatic | - | - | 1 => true; value != 0 => false |
decimal | int | int->fromDecimal | - | - | Returns the integer part. Any fractional part of this decimal will be discarded. No rounding. |
decimal | decimal | - | - | - | |
decimal | datetime | - | - | - | |
decimal | string | string->fromDecimal | - | - | See java documentation for BigDecimal.toString method |
decimal | boolean | automatic | - | - | 1 => true; value != 0 => false |
datetime | int | - | - | - | |
datetime | decimal | - | - | - | |
datetime | datetime | - | - | - | |
datetime | string | string->fromDateTime | - | - | The date in ISO8601 format. |
datetime | boolean | - | - | - | |
string | int | int->fromString | string->isInt | yes | |
string | decimal | decimal->fromString | string->isDecimal | yes | |
string | datetime | datetime->fromString | string->isDateTime | yes | true => if the string is a valid ISO8601 date, false otherwise |
string | string | - | - | - | |
string | boolean | bool->fromString | - | - | |
bool | int | automatic | - | - | true => 1; false => 0 |
bool | decimal | automatic | - | - | true => 1; false => 0 |
bool | datetime | - | - | - | |
bool | string | automatic | - | - | true => "true"; false => "false" |
bool | boolean | - | - | - |
Complex scalars can be implicitly converted by the compiler or runtime engine only if all the properties of the destination type are found in the source type with the same name and type.
The following subchapters present all the possible type convertion situations and the ways you can use type conversions.
The coversion is automatic, properties m1, m2, m3 from source will be set in destination.
The coversion is automatic, properties m1, m2, m3 from source will be set in destination except for m4 which will be ignored.
Conversion must be handled by the developer because destination type has more properties than source type.
Conversion must be handled by the developer because destination type has different property names than source type.
Conversion must be handled by the developer because destination type has different property types than source type.