ABAP text field literal vs text string literal

Have you ever had a variable containing text? I am sure you did and regularly do. What is your preferred way to do declare it?

If you don't know or use the second option, then this post is for you. There is a minute difference in the way text literal has been defined in options one and two. 
First of all the declaration is a literal being passed to a variable, and some of you may ask:

what is a literal?

    Literals are defined in the source code of a program by specifying a string representing a value.
    Possible literals are numeric literals and character literals. The character literal comprises text 
    field literals and text string literals.

So there are two kinds of text literal, let's see them in detail.

Text Field Literals:

  - Defined using single inverted commas (').
  - Data type is c with the length of the included characters, including trailing blanks.
  - There is no empty text field literal:
     This means, The text field literal '' is identical to the text field literal ' ' of length 1.
   - The length of the text field literal( not of type c) is from 1 to 255.

Text String Literals:    

   - Defined using single backquotes (`).
   - Data type is a string.
   - The empty text string literal `` represents a string of length 0.
   - The length of the text string literal( not of type string) is from 0 to 255.
   - Trailing blanks are not ignored.

Alright, now that you know the difference, guess the output of this code.
The unexpected output is the manifestation of ABAP comparison rules of string to char or vice versa.

Armed with the new knowledge, the old example can be rewritten like this:

 In Conclusion:

   Appropriate text literal should be used when defining them to avoid superfluous type conversions. 
   The type to be used is determined by what type of variables they are going to be assigned. 
   Hope you liked the post and it helps you in your day to day work. Here is a good use example to
   sign off.

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

Application Logging in ABAP