My question is just as the title has, What's the difference between =
and ?=
in ABAP operators?
4 Answers
?=
is the (down)casting operator. It's used for assignments between reference variables, whose assignability is checked as early as the runtime starts.
C.f. general explanation at wikipedia.
Example:
DATA fruit TYPE REF TO zcl_fruit.
DATA apple TYPE REF TO zcl_apple. " inherits from zcl_fruit
DATA apricot TYPE REF TO zcl_apricot. " inherits from zcl_fruit
...
case fruit->type.
when 'apple'.
apple ?= fruit.
seeds = apple->seeds.
when 'apricot'.
apricot ?= fruit.
seeds = VALUE #( ( apricot->kernel ) ).
endcase.
Since 7.40, the constructor operator CAST
may be used:
DATA fruit TYPE REF TO zcl_fruit.
...
case fruit->type.
when 'apple'.
seeds = CAST zcl_apple( fruit )->seeds.
when 'apricot'.
seeds = VALUE #( ( CAST zcl_apricot( fruit )->kernel ) ).
endcase.

- 11,934
- 5
- 22
- 48

- 13,909
- 12
- 65
- 76
-
2Note that it is mainly used to cast different types of reference variables. When assigning between the same type of reference variable you can still use = – Esti Aug 29 '09 at 03:33
-
I just want to add this operator is used mostly in downcast, when you assign a superclass obj to a reference of a more specfic subclass, it requires an explicit ?= (castING operator) as this can lead to a runtime error: subclass ?= superclass – KurzedMetal Jan 09 '13 at 12:36
?= is used to refer to a super class object by its inherited class object in the form
[object reference of parent class] ?= [object reference of inherited class]
This is useful when the type resolution occurs at runtime. While ?= can be specified for upcasts also, it is not usually necessary.

- 37
- 6
It is Casting operator (?=) for assignments between reference variables,but specifically speaking it is down casting operator .

- 224
- 1
- 6
?= is used to type cast an object reference of an inherited class to an object of the super class from which it is derived.
?=
Type casting helps you to refer several object references of sub classes whose type is resolved only at run time. The parent class object reference can hold the objects and often there would be a method of parent class which can be used to determine what sub class object the type cast reference is holding at run time.

- 1