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 Type | Description | Initial Size | Valid Size | Initial Value |
|---|---|---|---|---|
| C | Alphanumeric characters | 1 character | 1 – 65,535 characters | space |
| N | Digits 0-9 | 1 character | 1 – 65,535 characters | Character zero |
| D | Date character YYYMMDD | 8 characters | 8 characters | Character zeros |
| T | Time character HHMMSS | 8 characters | 8 characters | Character zeros |
| X | Hexadecimal field | 1 byte | 1 – 65,535 bytes | |
| P | Packed Numbers | 8 bytes | 1 – 16 bytes | 0 |
| I | integers range 2^31-1 to 2^31 | 4 bytes | 4 bytes | 0 |
| F | 8 bytes | 8 bytes | 0 | |
| STRING | Like c, but has the variable length | |||
| XSTRING | like 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_nameFor 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.