Get Started with ABAP Programming on SAP BTP

Working With Basic Data Objects and Data Types

Objectives
After completing this lesson, you will be able to:

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.

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.

Sources of ABAP Data Types

ABAP offers different sources of data types.

Try It Out: Some Predefined ABAP TYPES

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
     * 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(  '---------' ).
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. 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.

Try It Out: Data Objects

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
     * 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                  ).
    
    Copy code
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. 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.
    • ...

Assigning Values 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 know what is an inline declaration in value assignments.

Save progress to your learning plan by logging in or creating an account