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

  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
    12345678910111213141516171819202122232425
    * 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( '---------' ).
  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.

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

  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
    1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
    * 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 ).
  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.
    • ...

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