Type conversion
Methods for converting objects from one type to another one, are used by the system during execution of the program, and can be executed:
Type conversion methods are integral parts of execution software, and mostly are internally implemented,
but it is possible to implement type conversion methods for the objects of own class (see below).
Type conversion algorithm performs conversion iteratively, until it receives the object of the expected type.
For more about data types see Data types.
Conversion by assigning to variable
A variable can has assigned type in its declaration (see Variables), for example:
is a declaration of variable
Examples of conversion by assigning:
In case of variables from parameters of a fuction, the type conversion will be executed
before the function execution.
For example, in this code:
a string value
An operators also converts its parameters to expected type, before executing operation, for example:
Notice, that conversion of values of parameters will execute appropriate type conversion methods
before executing the function code, but still inside given function,
what means, that in example, in case of a monitor method, conversion of parameters
will be executed inside a monitor lock grabed by that function,
and in the case of
For more about parameters see Parameters of function.
Conversion by statement
Entering expressions in some designated places of language statements,
will also executes appropriate type conversion method.
For example, in the statements:
it will convert expression
Conversion by type cast operator
Type cast operator is an unary operator, consisting type in brackets
For example:
converts value of the variable
When name of the type may be interpreted by the compiler as something else,
for example, when some variable also using the name,
than expression of type cast operator
It is also allowed to use
For priority of type cast operator in expression see Priority in expression.
For
Type conversion methods in classes
Type conversion methods can be defined inside class,
allowing objects of given class to be converted to other types.
Example:
above conversion methods, defined in the class
Notice, that conversion process will always be performed iteratively,
until the object of expected type will be received,
so it is allowed to return from conversion method with objects of other type,
knowing that it will be converted again.
The wanted type conversion method for an object of a class, will be searched in all inheritance chain.
For more about classes and inheritance chain see Classes and Class inheritance.