Summary -
In this topic, we described about the below sections -
What is Literal?
Literal is a data object that specifies character like values. Literal defined in the source code of the program. Literals are fully defined by their values.
The value of the literal can’t be changed. The fixed values assigned to a constant can also consider as literals. There are two types of literals and those are -
- Numeric literals
- Character literals
Numeric literals -
Numeric literals are continuous sequences of numbers(0 to 9). Numeric literals can have directly prefixed by a optional plus (+) or minus (-) sign. Numeric literals have no decimal separators.
Numeric literals have no exponent and no mantissa. The numeric literals are supported by either Integer(i) or packed (p) data types.
Numeric literals consist of number sequences upto a maximum of 31 digits. Numeric literals having value between -2147483648 (-2 31+1) and 2147483647 (2 31-1) are integer literals of data type integer(I).
If trying to specify the numeric literal value less than -2147483648 and greater than 2147483647, overflow error occured while activating the program object.
Numeric literals outside of above interval are packed numeric literals of data type packed(p). Numeric literals with a length of 8 bytes supports upto 15 digits and with a length of 16 bytes supports between 16 and 31 digits long.
For example, declare a variable like below with a value greater than 2147483649.
DATA v_numlit TYPE I VALUE 2147483649.
When tries to activate the program object, the below error gets occured.
Change the data type to Packed-decimal(P) like below, then the error gets resolved.
DATA v_numlit TYPE P VALUE 2147483649.
- The conversion operator CONV used to convert a numeric literal to the type int8 as there are no numeric literals of type int8.
- ither decimal separators or scientific notation with mantissa and exponent not allowed to use in numeric literals.
- Numeric literals that span multiple lines are not permitted.
- The literal operator '&' cannot be used to create a composite literal from multiple numeric literals.
- Numbers can also be specified as character literals. If those character literals used in numeric value expected operand positions, they are converted accordingly. The conversion operator CONV is recommended for targeted conversions.
Character literals -
Character literals are sequence of alphanumeric characters. Character literals enclosed with single quotation marks('). Character literals can be of two types and those are -
- Text field literals
- Text string literals
Text field literals -
Text field literals are sequence of alphanumeric characters and enclosed with single quotation marks (').
Text field literal should not be empty (For example, the text field literal '' has the same meaning as the text field literal ' ' with length 1).
Text field literals supported data type is character ('C') with the length of the enclosed characters (including trailing blanks). There is no need to specify the length of the literal and the length automatically gets calculated based on number characters provided. The text field literal length must be in between 1 to 255 characters.
Two consecutive quotation marks must be specified to represent a quotation mark in a text field literal if exists.
Text string literals -
Text string literals are sequence of alphanumeric characters and enclosed with single backquotes (`). Text string literal should not be empty (For example, the text string literal `` is an empty string of length 0).
Text string literals supported data type is 'string'. There is no need to specify the length of the literal and the length automatically gets calculated based on number characters provided. The text string literal length can be maximun of 255 characters.
Two consecutive backquotes must be specified to represent a backquote in a text string literal if exists.
- Character literals that span multiple lines are not allowed.
- Only text field literals (not text string literals) used to associate a literal with a text symbol.
- The literal operator '&' can be used to join multiple literals with the same type as a composite literal.
Example -
Code a program to show the declaration of all literal types and display them.
Code -
*&---------------------------------------------------------------------*
*& Report Z_LITERAL
*&---------------------------------------------------------------------*
*& Program written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_LITERAL.
* Declaring Numeric literal 3.141 as lv_nliteral
DATA lv_nliteral TYPE P LENGTH 5 DECIMALS 3 VALUE 3.141.
* Declaring text field lteral "Hello World" as lv_tfl
DATA lv_tfl(20) TYPE C VALUE 'Hello World'.
* Declaring text string lteral `Welcome to TutorialsCampus` as lv_tsl
DATA lv_tsl TYPE string VALUE `Welcome to TutorialsCampus`.
* Display all types of literal in different lines.
WRITE: 'Numeric literal is - ',lv_nliteral,
/ 'Text field literal is - ',lv_tfl,
/ 'Text string literal is - ',lv_tsl.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
lv_nliteral variable is declared with numeric literal 3.141. lv_tfl variable declared with length of 20 and initialized with "Hello Word". In this declaration, we used single quotation marks('). So it is text character literal declaration. lv_tsl variable declared with length of 20 and initialized with "Welcome to TutorialsCampus". In this declaration, we used single single backquotes(`). So it is text string literal declaration.