Working With Basic Data Objects and Data L3
Working With Basic Data Objects and Data Types
Objectives
After completing this lesson, you will be able to:
Declare data objects.
Assign values.
Data Objects in ABAP
A data object in an ABAP program represents a reserved section of the program memory.
ABAP knows three types of data objects: Variables, Constants, and Literals.
Variables
A variable is a data object with content that can change during runtime. A variable is identified by a name. The name is also used to address the data object at runtime. The starting value of an ABAP variables is always well-defined.
Constants
Constants are similar to variables. But in contrast to variables the value is hard coded in the source code and must not change during runtime. Like variables, constants have a name by which they can be re-used.
Literals
The value of literals is also hard-coded in the source code. In contrast to constants, literals don't have a name. Because of that you cannot reuse a literal. Only use literals to specify the values for constants and the starting values for variables.
ABAP data objects are always typed: Every data object is based on a data type which determines the kind of information they can contain. The data type of an ABAP data object stays the same throughout a program execution.
Declaration of Variables
A variable in an ABAP program is declared with keyword DATA.
A DATA statement consists of three parts. Let's look at each part in more detail.
DATA
Keyword DATA is followed by the name of the variable. The name of a variable may be up to 30 characters long. It may contain the characters A-Z, the digits 0-9, and the underscore character. The name must begin with a letter or an underscore.
TYPE
The type of the variable is specified after addition TYPE. In the example, built-in types i (for integer numbers) and string (character string with variable length) are used.
VALUE
Addition VALUE is optional and you can use it to specify a start value for the variable. If VALUE is missing, the variable is created with an initial value that depends on the technical type of the variable.
Sources of ABAP Data Types
ABAP offers the following sources of Data Types:
ABAP Built-in
ABAP has a set of 13 predefined data types for simple numeric, char-like, and binary data objects.
TYPES Statement
Statement TYPES allows you to define data types and reuse them in different places, depending on the location of the definition.
ABAP Dictionary
The ABAP Dictionary is a part of the ABAP Repository. Among other things, it manages global data types which are available throughout the system. ABAP Dictionary types not only define technical properties, they add semantic information, for example, labels. ABAP Dictionary types are particularly useful when implementing user interfaces.
Try It Out: Predefined ABAP TYPES
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code Snippet
Copy code
Switch to dark mode
* Data Objects with Built-in Types
**********************************************************************
" comment/uncomment the following declarations and check the output
DATA variable TYPE string.
* DATA variable TYPE i.
* DATA variable TYPE d.
* DATA variable TYPE c LENGTH 10.
* DATA variable TYPE n LENGTH 10.
* DATA variable TYPE p LENGTH 8 DECIMALS 2.
* Output
**********************************************************************
out->write( 'Result with Initial Value)' ).
out->write( variable ).
out->write( '---------' ).
variable = '19891109'.
out->write( 'Result with Value 19891109' ).
out->write( variable ).
out->write( '---------' ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app.
Analyze the console output. Uncomment different declarations of variable. Try your own declarations to get familiar with the concepts.
Constants and Literals
Declaration of Constants
A constant is a data object with a hard-coded value that must not be changed during runtime. Any write access to a constant leads to a syntax error.
In ABAP, you declare a constant using keyword CONSTANTS. A CONSTANT statement consists of the same parts as a DATA statement. The only difference is, that the VALUE addition is mandatory.
You can use the VALUE addition in the special form VALUE IS INITIAL, if the value of the constant should be the type-specific initial value.
Literals in ABAP
Literals are anonymous data objects with a hard-coded value. Literals are often used to define non-initial values for constants and non-initial starting values for variables.
Technically, you can use literals anywhere in your code. To support readability and maintainability, it is recommended to define and use constants, instead.
ABAP knows three types of literals:
Number Literals are integer numbers with or without sign. Number literals usually have data type I. Only if the value is too large for data type I, type P is used, with a sufficient length and without decimal places.
Text Literals are character strings in simple quotes. Text literals have type C. The length is derived from the content in quotes. Trailing blanks are ignored.
String Literals are character strings in a pair of back quotes (`). String literals are of type STRING. They should be used to provide values for string typed data objects.
Try It Out: Data Objects
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
Code Snippet
Copy code
Switch to dark mode
* Example 1: Local Types
**********************************************************************
* Comment/Uncomment the following lines to change the type of my_var
TYPES my_type TYPE p LENGTH 3 DECIMALS 2.
* TYPES my_type TYPE i .
* TYPES my_type TYPE string.
* TYPES my_type TYPE n length 10.
* Variable based on local type
DATA my_variable TYPE my_type.
out->write( my_variable (TYPE MY_TYPE) ).
out->write( my_variable ).
* Example 2: Global Types
**********************************************************************
* Variable based on global type .
" Place cursor on variable and press F2 or F3
DATA airport TYPE /dmo/airport_id VALUE 'FRA'.
out->write( airport (TYPE /DMO/AIRPORT_ID ) ).
out->write( airport ).
* Example 3: Constants
**********************************************************************
CONSTANTS c_text TYPE string VALUE Hello World.
CONSTANTS c_number TYPE i VALUE 12345.
"Uncomment this line to see syntax error ( VALUE is mandatory)
* constants c_text2 type string.
out->write( c_text (TYPE STRING) ).
out->write( c_text ).
out->write( '---------' ).
out->write( c_number (TYPE I ) ).
out->write( c_number ).
out->write( --------- ).
* Example 4: Literals
**********************************************************************
out->write( '12345 ' ). "Text Literal (Type C)
out->write( 12345 ). "String Literal (Type STRING)
out->write( 12345 ). "Number Literal (Type I)
"uncomment this line to see syntax error (no number literal with digits)
* out->write( 12345.67 ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app.
Play around with the source code to get familiar with the concepts.
Uncomment different declarations of my_type and use F2 and F3 to analyze the definition of variable my_variable.
Use F2 and F3 to analyze the definition of variable airport.
Uncomment the definition of constant c_text2 to see the syntax error.
Uncomment the last code line to see the syntax error.
...
Value Assignments to Data Objects
Use value assignments to change the value of variables.
Watch this video to see how.
Implicit Type Conversions
Although ABAP is a typed programming language, in a value assignment the type of the source expression and the type of the target variable can be different. If that is the case, the runtime attempts in a type conversion.
As shown in the figure, if possible, try to avoid type conversions for the following reasons:
Additional Runtime consumption: Values with type conversions require more runtime than value assignments with identical types.
Potential Runtime Errors: Some combinations of source type and target type can lead to runtime errors. If, for example, the target variable has a numeric type l and the source expression has a character like type, then the runtime will raise an error if it cannot translate the text into a number.
Potential Information Loss: Some combinations of source type and target type do not cause runtime errors but can lead to a loss of data. If, for example, source and target are of both of type C but the type of the target variable is shorter. Then the runtime simply truncates the value of the source.
Resetting Variables
Statement CLEAR is a special kind of value assignment. It is used to reset a variable to its type-specific initial value.
Note
The CLEAR statement disregards the starting value from the VALUE addition. After CLEAR the variable always contains the type-specific initial value.
Inline Declarations in Value Assignments
Watch this video to learn about inline declarations and how they work in value assignments.
Log in to track your progress & complete quizzes
Log in
Register
Was this lesson helpful?
Yes
No
Next lesson
Learning
Quick links
Learning Support
About SAP
Site Information
Working With Basic Data Objects and Data Types
Learning /Browse/Courses/Learning Basic ABAP Programming/Working With Basic Data Objects and Data Types Working With Basic Data Objects and Data Types Objectives After completing this lesson, you will be able to: Declare data objects. Assign values.
Data Objects in ABAP
A data object in an ABAP program represents a reserved section of the program memory. ABAP knows three types of data objects:
Variables: A variable is a data object with content that can change during runtime. It is identified by a name and serves as a placeholder for dynamic values. The name is used to address the data object at runtime. The starting value of an ABAP variable is always well-defined.
Constants: Constants are similar to variables but their value is hard-coded in the source code and must not change during runtime. Any attempt to modify a constant's value results in a syntax error. Like variables, constants have a name by which they can be re-used.
Literals: The value of literals is also hard-coded directly in the source code. In contrast to constants, literals don't have a name, meaning they cannot be reused directly. They are primarily used to specify the values for constants and the starting values for variables.
ABAP data objects are always typed: Every data object is based on a data type which determines the kind of information they can contain and how much memory they occupy. The data type of an ABAP data object stays the same throughout a program execution.
Declaration of Variables
A variable in an ABAP program is declared with keyword DATA. A DATA statement consists of three parts:
DATA Keyword:
DATAis followed by the name of the variable. The name of a variable may be up to 30 characters long. It may contain the characters A-Z, the digits 0-9, and the underscore character (_). The name must begin with a letter or an underscore.TYPE: The type of the variable is specified after addition
TYPE. This type determines the data category (e.g., numeric, character, string), its length, and its behavior. In the example, built-in typesi(for integer numbers) andstring(character string with variable length) are used.VALUE: Addition
VALUEis optional and you can use it to specify a start value for the variable. IfVALUEis missing, the variable is created with an initial value that depends on its technical type. For example, numeric types (i,p,f) are initialized with 0, character-like types (c,n,d,t) with spaces, and string types (string,xstring) with an empty string ('').
Sources of ABAP Data Types
ABAP offers the following sources of Data Types:
ABAP Built-in: ABAP has a set of 13 predefined data types for simple numeric, character-like, and binary data objects.
TYPES Statement: Statement
TYPESallows you to define local data types and reuse them within a specific program unit (e.g., a class or a report). The scope of these types depends on where they are defined.ABAP Dictionary: The ABAP Dictionary is a part of the ABAP Repository. Among other things, it manages global data types which are available throughout the system. ABAP Dictionary types not only define technical properties, they add semantic information, for example, labels. ABAP Dictionary types are particularly useful when implementing user interfaces.
Try It Out: Predefined ABAP TYPES
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main():
Code Snippet
Copy code
Switch to dark mode
* Data Objects with Built-in Types
****************************************************************************************************
" comment/uncomment the following declarations and check the output
DATA variable TYPE string.
* DATA variable TYPE i.
* DATA variable TYPE d.
* DATA variable TYPE c LENGTH 10.
* DATA variable TYPE n LENGTH 10.
* DATA variable TYPE p LENGTH 8 DECIMALS 2.
* Output
****************************************************************************************************
out->write( 'Result with Initial Value)' ).
out->write( variable ).
out->write( '---------' ).
variable = '19891109'.
out->write( 'Result with Value 19891109' ).
out->write( variable ).
out->write( '---------' ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Analyze the console output. Uncomment different declarations of variable. Try your own declarations to get familiar with the concepts.
Constants and Literals Declaration
Declaration of Constants
A constant is a data object with a hard-coded value that must not be changed during runtime. Any write access to a constant leads to a syntax error. In ABAP, you declare a constant using keyword CONSTANTS. A CONSTANTS statement consists of the same parts as a DATA statement. The only difference is, that the VALUE addition is mandatory. You can use the VALUE addition in the special form VALUE IS INITIAL, if the value of the constant should be the type-specific initial value (e.g., 0 for numeric types, spaces for character types).
Literals in ABAP
Literals are anonymous data objects with a hard-coded value. Literals are often used to define non-initial values for constants and non-initial starting values for variables. Technically, you can use literals anywhere in your code. To support readability and maintainability, it is recommended to define and use constants, instead.
ABAP knows three types of literals:
Number Literals: These are integer numbers with or without a sign. Number literals usually have data type
I(integer). Only if the value is too large for data typeI, typeP(packed number) is used, with a sufficient length and without decimal places. Examples:123,-456.Text Literals: These are character strings enclosed in single quotes (
'). Text literals have typeC(character). The length is derived from the content in quotes, and any trailing blanks are ignored. For example,'HELLO 'is interpreted as a text literal of length 5 with the value 'HELLO'.String Literals: These are character strings enclosed in a pair of back quotes (
`). String literals are of typeSTRING. They should be used to provide values for string typed data objects and preserve all characters, including trailing blanks. For example,`Hello World `will retain all trailing spaces.
Try It Out: Data Objects
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main():
Code Snippet
Copy code
Switch to dark mode
* Example 1: Local Types
****************************************************************************************************
* Comment/Uncomment the following lines to change the type of my_var
TYPES my_type TYPE p LENGTH 3 DECIMALS 2.
* TYPES my_type TYPE i .
* TYPES my_type TYPE string.
* TYPES my_type TYPE n length 10.
* Variable based on local type
DATA my_variable TYPE my_type.
out->write( `my_variable (TYPE MY_TYPE)` ).
out->write( my_variable ).
* Example 2: Global Types
****************************************************************************************************
* Variable based on global type . " Place cursor on variable and press F2 or F3
DATA airport TYPE /dmo/airport_id VALUE 'FRA'.
out->write( `airport (TYPE /DMO/AIRPORT_ID )` ).
out->write( airport ).
* Example 3: Constants
****************************************************************************************************
CONSTANTS c_text TYPE string VALUE `Hello World`.
CONSTANTS c_number TYPE i VALUE 12345.
"Uncomment this line to see syntax error ( VALUE is mandatory)
* constants c_text2 type string.
out->write( `c_text (TYPE STRING)` ).
out->write( c_text ).
out->write( '---------' ).
out->write( `c_number (TYPE I )` ).
out->write( c_number ).
out->write( `---------` ).
* Example 4: Literals
****************************************************************************************************
out->write( '12345 ' ). "Text Literal (Type C)
out->write( `12345 ` ). "String Literal (Type STRING)
out->write( 12345 ). "Number Literal (Type I)
"uncomment this line to see syntax error (no number literal with digits)
* out->write( 12345.67 ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Play around with the source code to get familiar with the concepts. Uncomment different declarations of my_type and use F2 and F3 to analyze the definition of variable my_variable. Use F2 and F3 to analyze the definition of variable airport. Uncomment the definition of constant c_text2 to see the syntax error. Uncomment the last code line to see the syntax error.
Value Assignments to Data Objects
Use value assignments to change the value of variables.
Implicit Type Conversions
Although ABAP is a typed programming language, in a value assignment the type of the source expression and the type of the target variable can be different. If that is the case, the runtime attempts an implicit type conversion. As shown in the figure, try to avoid type conversions for the following reasons:
Additional Runtime consumption: Values with type conversions require more runtime than value assignments with identical types. The ABAP runtime needs to execute specific conversion routines at the CPU level, which consumes additional processing time.
Potential Runtime Errors: Some combinations of source type and target type can lead to runtime errors. For example, if the target variable has a numeric type
iand the source expression has a character-like type like'ABC', the runtime will raise aCX_SY_CONVERSION_NO_NUMBERerror if it cannot translate the text into a number.Potential Information Loss: Some combinations of source type and target type do not cause runtime errors but can lead to a loss of data. For example, if both source and target are of type
Cbut the target variable's length is shorter (e.g.,DATA(lv_char5) TYPE c LENGTH 5. lv_char5 = 'ABCDEFGHIJ'.), then the runtime will simply truncate the value of the source, resulting inlv_char5containing'ABCDE'. Similarly, assigning a value12345to a packed numberDATA(lv_p) TYPE p LENGTH 2 DECIMALS 0would result in an overflow.
Resetting Variables
Statement CLEAR is a special kind of value assignment. It is used to reset a variable to its type-specific initial value. This is the same initial value that the variable would have if the VALUE addition was omitted during its declaration (e.g., 0 for numbers, spaces for characters, empty string for strings).
Note: The CLEAR statement disregards any starting value from the VALUE addition. After CLEAR, the variable always contains the
Learning /Browse/Courses/Learning Basic ABAP Programming/Working With Basic Data Objects and Data Types Working With Basic Data Objects and Data Types Objectives After completing this lesson, you will be able to: Declare data objects. Assign values.
Data Objects in ABAP
A data object in an ABAP program represents a reserved section of the program memory. ABAP knows three types of data objects:
Variables: A variable is a data object with content that can change during runtime. It is identified by a name and serves as a placeholder for dynamic values, such as user input, calculation results, or temporary storage. The name is used to address the data object at runtime. The starting value of an ABAP variable is always well-defined.
Use Cases: Storing intermediate results in complex calculations, holding values retrieved from databases, or capturing user choices in interactive programs.
Benefits: Provides flexibility in program execution by allowing values to be dynamically manipulated.
Constants: Constants are similar to variables but their value is hard-coded in the source code and must not change during runtime. Any attempt to modify a constant's value results in a syntax error. Like variables, constants have a name by which they can be re-used.
Use Cases: Defining fixed configuration settings (e.g., maximum number of iterations, application version numbers), mathematical constants (\pi), or error codes.
Benefits: Enhances code readability and maintainability by giving meaningful names to fixed values. Prevents accidental modification of critical values.
Literals: The value of literals is also hard-coded directly in the source code. In contrast to constants, literals don't have a name, meaning they cannot be reused directly. They are primarily used to specify the values for constants and the starting values for variables.
Use Cases: Direct assignment of simple values, like initial numbers (
0), single characters ('X'), or short texts ('OK'), whereCONSTANTSdeclaration might be seen as overhead for a one-time use.Benefits: Simplicity for inline, singular value assignments, reducing the need for separate declarations.
ABAP data objects are always typed: Every data object is based on a data type which determines the kind of information they can contain and how much memory they occupy. The data type of an ABAP data object stays the same throughout a program execution.
Declaration of Variables
A variable in an ABAP program is declared with keyword DATA. A DATA statement consists of three parts:
DATA Keyword:
DATAis followed by the name of the variable. The name of a variable may be up to 30 characters long. It may contain the characters A-Z, the digits 0-9, and the underscore character (_). The name must begin with a letter or an underscore.TYPE: The type of the variable is specified after addition
TYPE. This type determines the data category (e.g., numeric, character, string), its length, and its behavior. In the example, built-in typesi(for integer numbers) andstring(character string with variable length) are used.Benefits of Type Specification: Ensures memory efficiency by allocating only the necessary space, enforces data integrity by restricting the kind of values a variable can hold, and helps prevent runtime errors by enabling compile-time checks.
VALUE: Addition
VALUEis optional and you can use it to specify a start value for the variable. IfVALUEis missing, the variable is created with an initial value that depends on its technical type. For example, numeric types (i,p,f) are initialized with 0, character-like types (c,n,d,t) with spaces, and string types (string,xstring) with an empty string ('').Benefits of Initial Value: Prevents programs from operating on uninitialized data, which can lead to unpredictable behavior or errors. Improves code clarity by explicitly setting a base state for the variable.
Sources of ABAP Data Types
ABAP offers the following sources of Data Types:
ABAP Built-in: ABAP has a set of 13 predefined data types for simple numeric, character-like, and binary data objects.
Use Cases: Most common basic data storage, suitable for fundamental data like integers, dates, times, and strings.
Benefits: Universally available, simple to use, and directly supported by the ABAP runtime environment.
TYPES Statement: Statement
TYPESallows you to define local data types and reuse them within a specific program unit (e.g., a class or a report). The scope of these types depends on where they are defined.Use Cases: Creating custom data structures (like internal tables or complex work areas) tailored to specific application logic within a program. Defining aliases for existing types for better semantical meaning.
Benefits: Improves code modularity, readability, and consistency within a program unit. Facilitates the creation of complex data models without global dependencies.
ABAP Dictionary: The ABAP Dictionary is a part of the ABAP Repository. Among other things, it manages global data types which are available throughout the system. ABAP Dictionary types not only define technical properties, they add semantic information, for example, labels. ABAP Dictionary types are particularly useful when implementing user interfaces.
Use Cases: Defining reusable data elements and structures that are consistent across multiple programs, external interfaces, and database tables. Essential for integrating with SAP's data model.
Benefits: Ensures data consistency across the entire SAP system, provides central management of data definitions, and automatically supports user interface elements like input help (F4) and field labels, reducing development effort and improving user experience.
Try It Out: Predefined ABAP TYPES
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main():
Code Snippet
Copy code
Switch to dark mode
* Data Objects with Built-in Types
****************************************************************************************************
" comment/uncomment the following declarations and check the output
DATA variable TYPE string.
* DATA variable TYPE i.
* DATA variable TYPE d.
* DATA variable TYPE c LENGTH 10.
* DATA variable TYPE n LENGTH 10.
* DATA variable TYPE p LENGTH 8 DECIMALS 2.
* Output
****************************************************************************************************
out->write( 'Result with Initial Value)' ).
out->write( variable ).
out->write( '---------' ).
variable = '19891109'.
out->write( 'Result with Value 19891109' ).
out->write( variable ).
out->write( '---------' ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Analyze the console output. Uncomment different declarations of variable. Try your own declarations to get familiar with the concepts.
Constants and Literals Declaration
Declaration of Constants
A constant is a data object with a hard-coded value that must not be changed during runtime. Any write access to a constant leads to a syntax error. In ABAP, you declare a constant using keyword CONSTANTS. A CONSTANTS statement consists of the same parts as a DATA statement. The only difference is, that the VALUE addition is mandatory. You can use the VALUE addition in the special form VALUE IS INITIAL, if the value of the constant should be the type-specific initial value (e.g., 0 for numeric types, spaces for character types).
Literals in ABAP
Literals are anonymous data objects with a hard-coded value. Literals are often used to define non-initial values for constants and non-initial starting values for variables. Technically, you can use literals anywhere in your code. To support readability and maintainability, it is recommended to define and use constants, instead.
Recommendation: While literals offer direct value assignment, using named constants for any value that might be reused or holds specific business meaning significantly improves code readability and simplifies future modifications. If a literal's value needs to change, it must be updated everywhere it's used; a constant only needs one update.
ABAP knows three types of literals:
Number Literals: These are integer numbers with or without a sign. Number literals usually have data type
I(integer). Only if the value is too large for data typeI, typeP(packed number) is used, with a sufficient length and without decimal places. Examples:123,-456.Use Cases: Direct assignments of numeric values, e.g.,
out->write( 100 ).
Text Literals: These are character strings enclosed in single quotes (
'). Text literals have typeC(character). The length is derived from the content in quotes, and any trailing blanks are ignored. For example,'HELLO 'is interpreted as a text literal of length 5 with the value 'HELLO'.Use Cases: Short, fixed character strings, especially in older ABAP constructs, or when trailing spaces are intentionally ignored.
String Literals: These are character strings enclosed in a pair of back quotes (
`). String literals are of typeSTRING. They should be used to provide values for string typed data objects and preserve all characters, including trailing blanks. For example,`Hello World `will retain all trailing spaces.Use Cases: Modern character string manipulations where exact content, including leading/trailing spaces, is critical. Providing values for
stringtype variables and constants.
Try It Out: Data Objects
Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main():
Code Snippet
Copy code
Switch to dark mode
* Example 1: Local Types
****************************************************************************************************
* Comment/Uncomment the following lines to change the type of my_var
TYPES my_type TYPE p LENGTH 3 DECIMALS 2.
* TYPES my_type TYPE i .
* TYPES my_type TYPE string.
* TYPES my_type TYPE n length 10.
* Variable based on local type
DATA my_variable TYPE my_type.
out->write( `my_variable (TYPE MY_TYPE)` ).
out->write( my_variable ).
* Example 2: Global Types
****************************************************************************************************
* Variable based on global type . " Place cursor on variable and press F2 or F3
DATA airport TYPE /dmo/airport_id VALUE 'FRA'.
out->write( `airport (TYPE /DMO/AIRPORT_ID )` ).
out->write( airport ).
* Example 3: Constants
****************************************************************************************************
CONSTANTS c_text TYPE string VALUE `Hello World`.
CONSTANTS c_number TYPE i VALUE 12345.
"Uncomment this line to see syntax error ( VALUE is mandatory)
* constants c_text2 type string.
out->write( `c_text (TYPE STRING)` ).
out->write( c_text ).
out->write( '---------' ).
out->write( `c_number (TYPE I )` ).
out->write( c_number ).
out->write( `---------` ).
* Example 4: Literals
****************************************************************************************************
out->write( '12345 ' ). "Text Literal (Type C)
out->write( `12345 ` ). "String Literal (Type STRING)
out->write( 12345 ). "Number Literal (Type I)
"uncomment this line to see syntax error (no number literal with digits)
* out->write( 12345.67 ).
Press CTRL + F3 to activate the class and F9 to execute it as a console app. Play around with the source code to get familiar with the concepts. Uncomment different declarations of my_type and use F2 and F3 to analyze the definition of variable my_variable. Use F2 and F3 to analyze the definition of variable airport. Uncomment the definition of constant c_text2 to see the syntax error. Uncomment the last code line to see the syntax error.
Value Assignments to Data Objects
Use value assignments to change the value of variables.
Implicit Type Conversions
Although ABAP is a typed programming language, in a value assignment the type of the source expression and the type of the target variable can be different. If that is the case, the runtime attempts an implicit type conversion. As shown in the figure, try to avoid type conversions for the following reasons:
Additional Runtime consumption: Values with type conversions require more runtime than value assignments with identical types. The ABAP runtime needs to execute specific conversion routines at the CPU level, which consumes additional processing time that could be avoided with explicit, type-matching assignments.
Benefit of Avoiding: Faster program execution and better system performance, especially in high-volume operations.
Potential Runtime Errors: Some combinations of source type and target type can lead to runtime errors. For example, if the target variable has a numeric type
iand the source expression has a character-like type like'ABC', the runtime will raise aCX_SY_CONVERSION_NO_NUMBERerror if it cannot translate the text into a number. Another example is assigning a date string in an unexpected format.Benefit of Avoiding: Increased program stability and reliability, reducing unhandled exceptions and crashes.
Potential Information Loss: Some combinations of source type and target type do not cause runtime errors but can lead to a loss of data. For example, if both source and target are of type
Cbut the target variable's length is shorter (e.g.,DATA(lv_char5) TYPE c LENGTH 5. lv_char5 = 'ABCDEFGHIJ'.), then the runtime will simply truncate the value of the source, resulting inlv_char5containing'ABCDE'. Similarly, assigning a value12345to a packed numberDATA(lv_p) TYPE p LENGTH 2 DECIMALS 0could result in an overflow if the target cannot hold the value, or loss of decimal places when converting a packed number to an integer (i).Benefit of Avoiding: Ensures data integrity and accuracy throughout the program's execution, preventing silent errors that can lead to incorrect business logic or reports.
Resetting Variables
Statement CLEAR is a special kind of value assignment. It is used to reset a variable to its type-specific initial value. This is the same initial value that the variable would have if the VALUE addition was omitted during its declaration (e.g., 0 for numbers, spaces for characters, empty string for strings). CLEAR is crucial for ensuring that a variable starts with a clean slate before its next use.
Note: The CLEAR statement disregards any starting value from the VALUE addition. After CLEAR, the variable always contains the type-specific initial value.
Use Cases: Re-initializing variables inside loops (e.g., clearing a work area before processing the next row of an internal table), resetting counters, or ensuring input fields are empty after submission. This is particularly vital for variables that accumulate values or states over multiple operations.
Benefits: Prevents data residue or carry-over from previous operations, ensuring accurate and predictable program logic. Simplifies debugging by ensuring a known initial state for variables.