SAP ABAP: Data Types

Tram Ho

Author: ***[QuynhBC](https://www.facebook.com/bui.congquynh)*** Post: 03. Same as the article title. This article dives into the data types included in SAP ABAP. ## Data Types ### Data Type – Data Object * Elementary Type * Complex Data Type * Reference Type. ***1.Elementary Type*** Is the smallest data type in SAP and they are not composed of other types. They will be classified into fixed length and variable length basic types. * There are 13 fixed-length basic data types built into ABAP. There are eight types of numbers, namely integers (b, s, i, int8), decimal floating point numbers (decfloat16, decfloat34), binary floating point numbers (f), and packed numbers (p). There are two types of character likeness, the text field (c) and the numeric text field (n). There is a byte-like type, namely byte(x) fields. There are two types of date and time, the date field (d) and the time field (t). The data types c, n, x, and p are generic in length. p is also generic in terms of number of decimal places. The numeric data types b and s cannot be specified directly in programs intended for short integers. * There are two basic variable-length data types built into ABAP. There is a type like a character, which is a text string (string). There is a byte-like type, namely a byte string (xstring). For example: data: lv_c type c length 1, lv_string type string, lv_datetime type d. ***2.Reference Types*** * Describes data objects that contain references to other objects (data objects and instances of classes), called reference variable. There is no predefined reference type in ABAP. The reference type must be defined in the ABAP program or in the ABAP Dictionary. Reference types form a hierarchy, representing the hierarchy of objects to which references can point. For example: Data: go_object type ref to zclass. ***3.Complex Data Type*** * Including others. They allow the management and processing of semantically related datasets under one name. A data object of a composite type can be accessed globally or by component. Except for the **SY** structure, there are no complex data types built into ABAP. A complex type must be defined in the ABAP program or in the ABAP Dictionary. * There are 2 types of complex data types: * Structure Type: is a string of any basic data type, reference data type or complex data type. Structures are used to group work areas together logically. * Table Type: is a data type that includes many rows that have the same data type. To access Table Type, it is necessary to determine the KEY key of each table. For example: TYPES: BEGIN OF LTY_STRUC, LV_C TYPE C, "char LV_STR TYPE STRING, "string LV_D TYPE D, "date time END OF LTY_STRUC. "Structure type DATA: LT_TABLE TYPE TABLE OF LTY_STRUC. "Table type ### Object Type * Data types are templates for creating **data Objects**. Data types can be defined independently in the ABAP program or in the **ABAP Dictionary ** * As a property of a data object, data types can also exist in a non-independent state.Data types do not use any memory space for work data, but memory may be required for administrative information * It can be Class, Interface *For example below **Class** is initialized on Local or Global.* ***1.Local: Class is written in the same program*** REPORT ZPG_LOCAL_CLASS CLASS PERSON DEFINITION PUBLIC SECTION DATA: GV_NAME(20) TYPE C, GV_YEAR(4) TYPE C, GV_EMAIL(20) TYPE C. METHODS: CONSTRUCTOR IMPORTING IV_NAME LIKE GV_NAME IV_YEAR LIKE GV_YEAR IV_EMAIL LIKE GV_EMAIL METHODS: SET_AGE METHODS: DISPLAY PRIVATE SECTION DATA: GV_AGE(4) TYPE C. ENDCLASS CLASS PERSON IMPLEMENTATION METHOD CONSTRUCTOR GV_NAME = IV_NAME GV_YEAR. GV_EMAIL = IV_EMAIL. ENDMETHOD. METHOD SET_AGE. GV_AGE = SY-DATUM+0(4) - GV_YEAR. ENDMETHOD. METHOD DISPLAY. SET_AGE() . WRITE: / 'Name:',GV_NAME, / 'Year of birth:',GV_YEAR, / 'Email:',GV_EMAIL, / 'Age:',GV_AGE. ENDMETHOD. ENDCLASS. *DATA: gv_id TYPE i VALUE 0. CLASS STUDENT DEFINITION INHERITING FROM PERSON. PUBLIC SECTION. CLASS-DATA: No. TYPE I. DATA: GV_ID TYPE CHAR3. METHODS DISPLAY REDEFINITION. METHODS: CONSTRUCTOR IMPORTING IV_ID LIKE GV_ID IV_NAME LIKE GV_NAME IV_YEAR LIKE GV_YEAR IV_EMAIL LIKE GV_EMAIL. ENDCLASS. CLASS STUDENT IMPLEMENTATION. METHOD CONSTRUCTOR. STT = STT + 1. SUPER->CONSTRUCTOR( EXPORTING * GV_ID = IV_ID IV_NAME = IV_NAME IV_YEAR = IV_YEAR IV_EMAIL = IV_EMAIL ). ENDMETHOD. METHOD DISPLAY. WRITE: / 'STT:', No. WRITE: / 'ID:',GV_ID. SUPER->DISPLAY( ). ENDMETHOD. ENDCLASS. DATA: GO_STUDENT TYPE REF TO STUDENT, GO_PERSON TYPE REF TO PERSON. PARAMETERS: LV_NAME(20) TYPE C, LV_YEAR(4) TYPE C, LV_EMAIL(20) TYPE C, LV_ID TYPE CHAR3. START-OF-SELECTION. CREATE OBJECT GO_STUDENT EXPORTING IV_ID = LV_ID IV_NAME = LV_NAME IV_YEAR = LV_YEAR IV_EMAIL = LV_EMAIL. GO_STUDENT->DISPLAY( ). ***2.Class Global: Class is created on SE24 and called into the program when you want to use it*** First of all, you need to go to Tcode se24 => create ZCL_DEMO_STUDENT. ![](https://images.viblo.asia/b84e1d9d-0ca9-4383-b99c-a2931a727ef5.png) ![](https://images.viblo.asia/1ef735ef-dd41-40d8-a673-6c52ef225b3c. png) ![](https://images.viblo.asia/02698dfe-0ae0-4cc0-9d56-8cc859f6e2fd.png) ![](https://images.viblo.asia/27e078e4-82b5-4b91-9022- 7b593a05b8b1.png) REPORT ZPG_DEMO1. DATA: GO_OBJECT TYPE REF TO ZCL_DEMO_STUDENT. DATA: LV_INPUT TYPE STRING, LV_OUTPUT TYPE STRING. LV_INPUT = 'QUYNHBC'. CREATE OBJECT GO_OBJECT. CALL METHOD GO_OBJECT->GET_NAME EXPORTING IV_NAME = LV_INPUT " Class input IMPORTING EV_NAME = LV_OUTPUT." Output after running Class. WRITE: LV_OUTPUT.

Share the news now

Source : Viblo