ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Expressions and Functions for Conditions → log_exp - Logical Expressions → rel_exp - Comparison Expressions → rel_exp - Comparison Rules
rel_exp - Comparing Reference Variables
It is possible to compare data references with data references and to compare object references with object references, but it is not possible to compare data references with object references. Two reference variables are identical if they point to the same object. Size comparisons are defined internally and always produce similar results in similar situations.
The null reference in an initial reference variable is always smaller than any non-initial reference. However, a non-initial invalid reference that no longer points to an object cannot be compared. A comparison with a non-initial invalid reference always produces a non-handleable exception.
Other versions: 7.31 | 7.40 | 7.54
Notes
- For data references to be identical, the data type of the referenced objects must be
compatible and it is not enough for the operands just
to contain the same reference. If, for example, two reference variables have the same memory address,
and one points to a structure and the other to the first component of the structure, these variables
are not identical since the data type of the operands is incompatible. Reference variables filled using
GET REFERENCE
might not be identical, even though they point to the same data object, ifGET REFERENCE
or the reference operatorREF
is executed for a field symbol to which the data object was assigned using casting.
- The fact that non-initial invalid references cannot be compared affects all operations with internal
tables where comparisons take place, for example,
WHERE
conditions, sorts, or reads using a sorted key. If a comparison with a non-initial invalid reference takes place, this produces a runtime error.
Example
The following comparisons are both true. Although all references created with the reference operator
REF
point to the same data object in the memory, the reference created using <fs2>
is not the same as the other two, because it references another data type.
DATA dobj TYPE d.
ASSIGN dobj TO FIELD-SYMBOL(<fs1>).
IF REF #( dobj ) = REF #( <fs1> ).
...
ENDIF.
TYPES text TYPE c LENGTH 8.
FIELD-SYMBOLS <fs2> TYPE text.
ASSIGN dobj TO <fs2> CASTING.
IF REF #( dobj ) <> REF #( <fs2> ).
...
ENDIF.