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. |
- 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. |
- 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. |
- 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.