ABAP New Syntax Lecture 2: VALUE, NEW, CONV, CAST and REF

ABAP New Syntax Lecture 2 Constructor Expressions

Constructor expressions let you create values, objects, conversions and references directly where a result is required. In this lecture we will use VALUE, NEW, CONV, CAST and REF with practical ABAP examples.

Learning outcome: You will know what each operator returns, when # can infer a type, and when an explicit type improves safety and readability.

1. What is a constructor expression?

A constructor expression uses an operator plus a type and parentheses. Its result can be used on the right side of an assignment, as a method parameter or inside another expression.

DATA(result) = operator type( parameters ).

The placeholder # asks the compiler to derive the operand type from the surrounding context. Use it only when that context is unambiguous.

2. VALUE — construct structures and internal tables

VALUE creates a data value. It is especially useful for structures, ranges and internal tables because initialization stays close to the declaration.

TYPES: BEGIN OF ty_item,
         id   TYPE i,
         text TYPE string,
       END OF ty_item,
       ty_items TYPE STANDARD TABLE OF ty_item WITH EMPTY KEY.

DATA(ls_item) = VALUE ty_item(
  id   = 100
  text = 'Demo' ).

DATA(lt_items) = VALUE ty_items(
  ( id = 100 text = 'Demo' )
  ( id = 200 text = 'Practice' ) ).
ABAP VALUE and NEW constructor expression examples

BASE keeps existing rows

lt_items = VALUE #(
  BASE lt_items
  ( id = 300 text = 'Added with BASE' ) ).

BASE copies the starting value before the new components or rows are applied. Without it, you construct a fresh value.

3. NEW — create objects and anonymous data

NEW returns a reference. With a class it creates an object; with a data type it creates an anonymous data object.

DATA(lo_service) = NEW zcl_service(
  iv_mode = 'TEST' ).

DATA(lr_count) = NEW i( 10 ).
lr_count->* = lr_count->* + 1.

VALUE vs NEW: VALUE returns a data value, while NEW returns a reference to the created object or data object.

4. CONV — explicit data conversion

CONV converts one value to the requested data type. It is clearer than depending on an implicit conversion when the target type affects the result.

DATA(lv_number) = CONV i( '42' ).
DATA(lv_text)   = CONV string( lv_number ).

DATA(lv_ratio) = CONV decfloat34( 7 ) /
                 CONV decfloat34( 3 ).

Conversions can fail or lose information depending on the types and values involved. Validate external input before converting it.

5. CAST — change the static reference type

CAST works with references. A downcast should only be performed when the referenced object is compatible with the target class or interface.

IF lo_object IS INSTANCE OF zcl_child.
  DATA(lo_child) = CAST zcl_child( lo_object ).
  lo_child->run_child_logic( ).
ENDIF.

CONV converts data values; CAST changes the static type through which an existing reference is accessed.

6. REF — create a data reference

DATA(lv_total) = 1250.
DATA(lr_total) = REF #( lv_total ).

lr_total->* = 1500.
" lv_total is now 1500
ABAP CONV CAST and REF constructor expression examples

7. Choosing the correct operator

OperatorUseReturns
VALUEConstruct a valueData object
NEWCreate object or anonymous dataReference
CONVConvert a data valueConverted value
CASTCast an existing referenceTyped reference
REFReference an existing data objectData reference

8. Common mistakes

  • Using # where the compiler has no clear target type.
  • Using CAST without checking whether a downcast is valid.
  • Hiding important conversions in a very long nested expression.
  • Assuming NEW returns a value instead of a reference.
  • Replacing readable intermediate variables only to make the code shorter.

9. Practice exercises

  1. Create a structure and a three-row internal table with VALUE.
  2. Append one row while preserving existing rows with BASE.
  3. Create an anonymous integer with NEW i( ) and change its dereferenced value.
  4. Convert character input to i with suitable error handling.
  5. Explain the difference between NEW and REF in your own words.

10. Interview quick checks

Does VALUE create a reference?

No. It constructs a data value. Use NEW when the required result is a reference to a newly created object or anonymous data object.

What does # mean?

It requests type inference from the operand position. It is useful only when the required type is known from the surrounding context.

Are CONV and CAST interchangeable?

No. CONV performs a data conversion, whereas CAST works with reference typing.

Official SAP references

Examples are educational. Verify syntax availability, released APIs and data types in the ABAP Keyword Documentation for your target system.

Post a Comment

0 Comments