An implicit conversion exists from type S to type T if it is possible to assign an object of type S to an object of type T, with the equivalent of the following statement:
SET obj-T TO obj-S
The following types of implicit conversion are available:
This simply means that S is the same type as T
COBOL generally allows any numeric item to be assigned to any other, with any loss of data being the responsibility of the user. However, when determining if a conversion exists for the purposes of method overloading, we distinguish between proper conversions and truncation conversions. Proper conversions are those for which no loss of data magnitude should result, though even in this case precision can be lost when converting between fixed point and floating point.
For managed COBOL types, proper implicit conversions include:
binary-char |
|
binary-char unsigned |
The same as binary-char, plus:
|
binary-short |
|
binary-short unsigned |
The same as binary-short, plus:
|
binary-long |
|
binary-long unsigned |
The same as binary-long, plus:
|
binary-double |
|
binary-double unsigned |
The same as binary-double |
character |
|
float-short |
|
The value 0 may be converted to any enum type
If S and T are reference types, S may be assigned to T when:
Any value type can be converted to a java.lang.Object type by a process known as boxing. As part of this process the value is copied onto the object heap and a reference is created
Any numeric constant can be converted to any type capable of containing the full value of that constant. For instance the value 1 can be converted both to BINARY-CHAR UNSIGNED and to BINARY-CHAR.
A user defined implicit conversion consists of an optional implicit numeric conversion from S to S1 followed by the execution of a user defined implicit conversion operator resulting in T1, followed by another optional implicit numeric conversion to T. User defined implicit conversion operators are defined in COBOL with OPERATOR-ID IMPLICIT and result in methods with the name op_Implicit.