Difference between Chaining operator(&&) and Literal Operator(&)

Recently I talked about the Chaining Operator(&&) and how it simplifies dealing with texts and strings. I wanted to talk about a similar-looking operator with some similar functions to the chaining operator which makes it easy to confuse each other.

A literal operator(&) is used to join two or more literals to produce a single literal. However, there is a hard character length limit of 255.

Example of using a literal operator:

There are more features and limitations to a literal operator. Below are the points on which it's similar/different to a Chaining operator.

    - Similar to the chaining operator, you can use join string templates using &.

REPORT sy-repid.
DATA:
lv_string TYPE string,
lv_string2 TYPE string.
START-OF-SELECTION.
"Using Literal Operator
lv_string = |I want to use| & | literal Operator.| .
WRITE: lv_string. " Output: I want to use literal Operator.
"Using Chaining Operator
lv_string2 = |I want to use| && | Chaining Operator.| .
WRITE:/ lv_string2. " Output: I want to use chaining Operator.
view raw &&v&_ex1.abap hosted with ❤ by GitHub

    - Any trailing blank in text field literals is preserved, unlike chaining operator.

REPORT sy-repid.
DATA:
lv_string TYPE string,
lv_string2 TYPE string.
START-OF-SELECTION.
"Using Literal Operator
lv_string = 'Example using' & ' ' & 'literal Operator.' .
WRITE: lv_string. " Output: Example using literal Operator.
"Using Chaining Operator
lv_string2 = 'Example using' && ' ' && 'Chaining Operator.' .
WRITE:/ lv_string2. " Output: Example usingChaining Operator.
view raw &&v&_ex2.abap hosted with ❤ by GitHub

    - Can span multiline:

REPORT sy-repid.
DATA:
lv_string TYPE string.
START-OF-SELECTION.
lv_String = 'Test '
& 'Multiline '
& 'join.'.
write: lv_String. " Output -> Test Multiline join.

    - Calculated at compile-time.

Literal Operator compilation error

    - Can not have more than 255 characters(See the error in the above image), if you want to have a longer character,
      use the chaining operator.

    - Can not mix literal types while joining them using the literal operator.


- Can not use variables at operand positions


As you can see, the literal operator has a specific function i.e. to join literals and support multiline declarations while easy to confuse with the chaining operator.
In my opinion, One should refrain from using &(Literal Operator) and use chaining operator or string templates instead.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP