Difference Between && (Chaining Operator) and & (Literal Operator)

By | July 16, 2013 | Concepts | 20,912 | 4

With ABAP release 731, you have && which can be used instead of CONCATENATE. You also have & which seems to be doing the same job. So what is the difference?

Preface

You might think that there are more & in && than & :). But there are obvious difference between && and &:

Leandro asked in discussion on previous post ABAP String Templates New Feature in ABAP 731:

What’s the difference between & and &&?

It is very interesting question from an observant reader. Thanks Leandro! I thought the topic deserves more than a comment discussion.

Chaining Operator – &&

&& is used to CONCATENATE the character literals. This gets checked at run-time. Chaining operator && can accept any number of literals to build a continuous text.

Literal Operator – &

& is a literal operator. It would be used to define the string text across multiple program lines. It gets checked at compile time. Character literals joined using the literal operator & are also subject to the upper limit of 255 characters.

From Help

From ABAP help on Character String Operator

Do not confuse the chaining operator && with the literal operator &, which concatenates two character literals as a literal. The literal operator is generally used if you want to define a literal string template across multiple program lines. The operator is executed only once, when the program is compiled and trailing blanks of character literals are always respected. A character string expression with a chaining operator, on the other hand, is recalculated each time (like all expressions) and can be used to concatenate any number of character-like operands.

Few Examples

Example with same result:

Same results with & and &&

 
DATA  result TYPE string.
*
* multiple lines with &
result = |Hello| & | | 
          & |There| 
          & |!|.
write: / result.
* output = Hello There!
*
* Single line with &
result = |Hello| & | | & |There| & |!|.
write: / result.
* output = Hello There!
*
* Single line with &&
result = |Hello| && | | && |There| && |!|.
write: / result.
* output = Hello There!
 

Literal defined more than 255 using the literal operator will produce error during syntax check:

Compiler syntax error using & - literal operator

 
" each string part is 50 characters long
result =   `Long Long Long Long test line 1 with characters 50`
         & `Long Long Long Long test line 2 with characters 50`
         & `Long Long Long Long test line 3 with characters 50`
         & `Long Long Long Long test line 4 with characters 50`
         & `Long Long Long Long test line 5 with characters 50`
         & `Long Long Long Long test line 6 with characters 50`.
 

Dynamic text defined using the && will not complain at the compile time and will produce desired results

No syntax error using && - Chaining Operator

 
" each string part is 50 characters long
" with using && will not complain
result =   `Long Long Long Long test line 1 with characters 50`
        && `Long Long Long Long test line 2 with characters 50`
        && `Long Long Long Long test line 3 with characters 50`
        && `Long Long Long Long test line 4 with characters 50`
        && `Long Long Long Long test line 5 with characters 50`
        && `Long Long Long Long test line 6 with characters 50`.
 

More reading on the difference and similarity on ABAP help – String Templates

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

4 Comments

  • Leandro

    Hi Naimesh,

    It would be good if you posted in the code a comment giving the output, like:
    write …. ” outputs:

    I can’t test, but you didn’t mention one thing, so let me ask. Literal operator (&) can only be used for characters. It can’t be used for string expressions, because it’s resolved at compile time. Right?

  • Hello Leandro,

    I have updated the articles to show the output as well.

    Literal Operator can be used for the String expression as well. This code snippet would work same as using the Chaining Operator (&&). It doesn’t complain as at compile time, the length is not violating the any rule. At runtime, the substring is also not violating the rule as it is only 26 characters long before concatenating it into LV_STRING.

     
    data: lv_String type string.
    do 10 TIMES.
    lv_String = |{ lv_String }| & |{ sy-abcde }|.
    ENDDO.
    WRITE: lv_String.
     

    Thanks,
    Naimesh Patel

  • Sarap

    I’m just curious as to why SAP now has this.The standards/suggestions have been, to always use selection text or constants as opposed to text literals. Does this mean the issue with text literals consuming base memory resulting in dump is a thing of the past?

  • Hello Sarap,

    Apologies for being late – I was trying to find out the evidence on your question:
    Does this mean the issue with text literals consuming base memory resulting in dump is a thing of the past?.

    I couldn’t find any. Can you tell me where did you read this?

    Thanks,
    Naimesh Patel

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.