ABAP Data Types

Summary: in this tutorial, you will learn about the ABAP data types including elementary types, complex types, and reference types.

ABAP has three main data types: elementary types. complex types, and reference types.

Elementary types

The elementary type is further divided into the fixed-length type and variable-length type.

The fixed-length types in ABAP are C, D, N, T, F, I, P, and X. These types consist of:

  • Numeric types: I, F, P.
  • Character types: C, D, N, T.
  • Hexadecimal types: X

When you declare a data object that has a fixed-length type, you must specify its length in the variable declaration. For example:

data gv_name type c(50).

In this example, the length of the gv_name variable is 50. You cannot store more than 50 characters in the gv_name variable.

The variable-length types are string and xstring. When you declare a variable whose type is a variable-length type, you don’t need to declare its length. Its length varies during the execution of the program as you assign values to it. For example:

data gv_job_title type string.

In this example, the gv_job_title has the type of string. Its length will change once you assign data to it.

gv_job_title = 'SAP Developer'.

In this case, the length of gv_job_title is 13, which is the length of the string 'SAP Developer'.

The following table shows the characteristics of element types:

Data TypeDescriptionInitial SizeValid SizeInitial Value
CAlphanumeric characters1 character1 – 65,535 charactersspace
NDigits 0-91 character1 – 65,535 charactersCharacter zero
DDate character YYYMMDD8 characters8 charactersCharacter zeros
TTime character HHMMSS8 characters8 charactersCharacter zeros
XHexadecimal field1 byte1 – 65,535 bytes
PPacked Numbers8 bytes1 – 16 bytes0
Iintegers range 2^31-1 to 2^314 bytes4 bytes0
F8 bytes8 bytes0
STRINGLike c, but has the variable length
XSTRINGlike x, but has a variable length

Complex Types

The complex types are classified into structure types and table types.

Structure types

A structure is a group of related data objects. For example, you can have a structure address that contains house_no, street, city, state, zip_code and country.

A structure is an instance of a structure type. To define a structure type, you use the types statement. For example, the following defines a structure type called address_type

types: begin of address,
           house_no type c(15),
           street type c(120),
           city type c(50),
           state type c(50),
           zip_code type c(6),
           country type c(100),
       end of address.

The following declare two instances of the address type:

data: gv_sold_to_address type address,
      gv_ship_to_address type address.

You can access the individual field of a structure using the following syntax:

structure_name.field_name

For example:

gv_sold_to_address-house_no = 4000.
gv_sold_to_address-street = 'North 1st Street'.
gv_sold_to_address-city = 'San Jose'.
gv_sold_to_address-state = 'CA'.
gv_sold_to_address-zip_code = 95134.
gv_sold_to_address-country = 'USA'.

Internal Tables

An internal table consists of lines that have the same data type. The line of an internal table is a structure.

Typically, you use an internal table to process tabular data in ABAP programs.

An internal table is characterized by:

  • The line type.
  • A table key that uniquely identifies table rows. The table key can be unique or non-unique.
  • The table type e.g., hash tables, sorted index tables, and non-sorted index table.

The following shows how to define a table type called address_tab that stores a table of addresses:

types address_tab type standard table of address.

The following defines an instance of the address_tab type:

data gt_address type address_tab.

Reference types

The reference types describe reference variables that refer to instances of classes, interfaces, and runtime data objects.

The follownig shows how to define a reference of the cl_salv_table class:

data go_alv type ref to cl_salv_table.

We’ll discuss more the reference types in the ABAP objects.

Summary

  • ABAP data types include elementary type, complex types, and reference types.
  • Elementary types is divided into fixed-length type and variable-length type.
  • The complex types includes structure types and internal table types.