Modern ABAP introduced several features that make code cleaner and more expressive. One subtle but important difference is the choice between backticks (`) and single quotes (').
Although both represent text literals, they result in different data types, especially when using inline declarations.
Let's understand the difference.
Scenario
Consider the following inline declaration:
Using Backticks
DATA(lv_text) = `123`.
Result
lv_text is inferred as:
TYPE string
A dynamic-length string is created.
Using Single Quotes
DATA(lv_text) = '123'.
Result
lv_text is inferred as:
TYPE c LENGTH 3
A fixed-length character field is created.
1️⃣ What is the Difference?
✅ Backticks (`)
DATA(lv_text) = `SAP ABAP`.
Characteristics:
- Creates a STRING data type
- Dynamic length
- No predefined size limit (other than system limits)
- Ideal for modern ABAP development
- Preferred when working with dynamic text
✅ Single Quotes (')
DATA(lv_text) = 'SAP ABAP'.
Characteristics:
- Creates a fixed-length character field (TYPE C)
- Length is inferred from the literal
- Suitable for short, fixed-size values
- Traditional ABAP syntax
2️⃣ Why Does This Matter?
When using inline declarations, ABAP automatically determines the variable type based on the literal.
For example:
DATA(lv_string) = `Hello`.
Result:
TYPE string
Whereas:
DATA(lv_char) = 'Hello'.
Result:
TYPE c LENGTH 5
The syntax you choose directly affects the inferred data type.
3️⃣ Practical Differences
| Backticks (`) | Single Quotes (') |
|---|---|
Creates STRING | Creates TYPE C |
| Dynamic length | Fixed length |
| Modern ABAP style | Classic ABAP style |
| Best for variable-length text | Best for fixed values |
| Ideal for long text and concatenation | Useful for constants and short literals |
4️⃣ When Should You Use Each?
✅ Use Backticks When
- Working with dynamic text
- Using string templates
- Concatenating large strings
- Building JSON or XML payloads
- Writing modern ABAP code
Example:
DATA(lv_message) = `Order created successfully`.
✅ Use Single Quotes When
- Defining fixed character literals
- Comparing short constant values
- Working with legacy code
-
Assigning values to
TYPE Cfields
Example:
IF lv_status = 'A'. " Active ENDIF.
5️⃣ Does Assignment Still Work Between STRING and TYPE C?
Yes.
ABAP performs implicit conversions where appropriate.
Example:
DATA lv_char TYPE c LENGTH 10. DATA lv_string TYPE string. lv_string = 'SAP'. lv_char = `ABAP`.
The assignments are valid, but remember that TYPE C fields have a fixed length and may be padded with trailing spaces.
⚠️ Important Note About Trailing Spaces
One key difference is how trailing spaces are handled.
DATA(lv_char) = 'ABC'. DATA(lv_string) = `ABC`.
While both display the same value, a TYPE C field always has a fixed length and pads unused positions with spaces. A STRING stores only the actual content.
This distinction becomes important when performing string operations, comparisons, or interfacing with APIs.
๐ฏ Interview Tip
If an interviewer asks:
"What is the difference between backticks and single quotes in ABAP?"
A basic answer is:
"Backticks create a STRING, while single quotes create a character field."
A stronger answer is:
"In inline declarations, backticks infer a
STRING(dynamic-length text), whereas single quotes infer a fixed-lengthTYPE C. Choosing the appropriate literal improves readability and avoids unnecessary type conversions in Modern ABAP."
This shows that you understand both the syntax and its impact on data types.
๐ Key Takeaways
-
Backticks (
)** create a **STRING` (dynamic-length text). -
Single quotes (') create a
TYPE C(fixed-length character field). - Inline declarations infer the variable type from the literal you use.
- Prefer backticks for modern ABAP, dynamic text, JSON/XML payloads, and string manipulation.
- Use single quotes when working with fixed character values or legacy code.
๐ฌ Interview Challenge
What will be the inferred data type of the following variables?
DATA(lv_a) = `100`. DATA(lv_b) = '100'. DATA(lv_c) = |100|.
Can you identify the type of each variable and explain the difference?
Share your answer in the comments! ๐
No comments:
Post a Comment