Let us now look at the predefined ABAP type.
Predefined data types can be categorized as complete data types and incomplete data types. The complete types
can be used to type a data object directly while the incomplete types must be supplemented with a length
specification to create a complete type.
Each predefined data type has a characteristic initial value. This type-specific initial value plays an important
role in instantiating the data objects and executing the CLEAR statement.
The image on your screen shows you the description for each data type, the initial value it would take on
instantiation, its static length, and its attributes. You will notice that for the variable length, the length begins
with zero and is adjusted dynamically during runtime.
The generic types simple, numeric, clike, csequence and xsequence are available in SAP Web AS 6.10 and
later.
The runtime environment of ABAP provides predefined generic types in addition to the predefined ABAP
types.
Unlike the ABAP data type, the generic data type cannot be used to define a data object. They are used
exclusively to type formal parameters and field symbols. The third column in the image on your screen shows
you the compatibility of the generic data type with one or more predefined data types. For example, the generic
data type ‘numeric’ is only compatible with the ABAP data types i, f and p.
You have to specify start values for date or time fields as text literals.
ABAP has a separate data type for Date and Time fields. They are character like data types and
contain 8 numeric characters for the date and 6 numeric characters for the time.
On the screen you can see that the 8 character field for the data type date can be represented as the
date format or as an integer when the date type is converted to an integer for arithmetic purposes.
The internal format of YYYYMMDD can also be defined to a more user friendly format. This specified
format can be used for input and output. The sample code on your screen shows you how date1 has
a formatted output and date2 is as per the system format.
Date fields can be used in arithmetic expressions. The data object of type date will be represented as
an integer and its value is calculated as the number of days from 1st January 0001.
If the data object with type time is used in arithmetic expressions, then it takes a value as the number
of seconds from midnight.
If different types of ABAP data objects are used in an operation, the types are converted automatically.
Automatic type conversion is user friendly. However, conversions use up runtime and it is advisable to keep
conversion during runtime to a minimum.
Data types are descriptions in ABAP and are not linked to an address inside memory. Data objects are instances
of a data type and therefore occupy memory.
The image on your screen shows you the syntax to define a data object. The statement ‘DATA’ defines the data
and the keyword TYPE links the type to the data object. The link is static and cannot be changed at runtime.
Predefined data types are available in the ABAP runtime environment where you may have complete and
incomplete data types.
Global data types are defined in the Dictionary.
The local data type is defined using the TYPES statement within the ABAP program.
The structure of data objects can contain Elementary fields, Structures or Internal tables.
Structures and internal tables can be mutually nested to nearly any depth. If a structure contains an internal
table or other component with a variable length (components with type string and xstring), it is called a deep
structure. If the component has a fixed length, it is termed as a flat structure. A structure can contain other
structures as its components. Such objects are called nested structures.
To display a single quote within a text field literal, you have to enter it twice. The same applies to entering a
back quote in a string literal. No restrictions apply, however, to single quotes in string literal or back quotes in a
text field literal.
A literal is defined in the source text of the program. There are three types of literals:
- The numeric literal which is defined as a sequence of digits.
- The text-field literal which is defined as a sequence of characters using the inverted commas to
enclose the characters.
- The string literal which is defined using the character string defined within back quotes. The string
literal is only available from SAP Web Application Server 6.10 and later.
There are some rules you must be aware of when defining data statements:
- Between MODULE and ENDMODULE the defined object is still a global data object that is visible in
the whole program.
- Data objects declared in an ABAP event block are visible globally.
- Data objects that are created with the TABLES statement are always visible in the entire program, even
if the statement appears within a subroutine or a function module. (The objects can even be visible in other
programs, for example when calling an external subroutine).
As ABAP developers, you are familiar with the concept of encapsulating data. The extent to which
data objects are visible depends on the context. The following rules apply to data objects that are
defined using the DATA statement:
A global data object is visible to the entire program.
Inside a subroutine, if the data appears between FORM and ENDFORM, it is a local data object to
the subroutine.
If it appears between FUNCTION and ENDFUNCTION, then it defines a local data object of a
function module.
Data objects that are created in the declaration part of a class are called attributes.
Using these attributes you can decide on the visibility areas using Private, Protected or Public
attributes.
The image on your screen shows you different options for specifying visibility.
The lifetime of a data object depends on their visibility.
Global data objects are valid as long as the program is in memory.
Local data objects are valid within the subroutine or function module which creates it. These data
objects are lost once the runtime of the modularization unit is completed.
If you use the STATICS statement instead of DATA when you declare local data objects, their lifetime
is extended to the overall program lifetime. Therefore, such data objects have the same lifetime as
global data objects, but their visibility is still limited to the respective modularization unit.
At the start of its lifetime the data object is assigned an initial value. For Global data objects this initial
value is assigned when the program is loaded into memory. For local data objects the initial value is
assigned each time the subroutine or function module is called.
If the local data objects are declared with the STATICS statement, then the initial value is assigned
the first time they are called by the surrounding modularization unit.
The system will not reserve memory for elementary, variable-length data objects on entering the
validity area. The necessary memory is allocated only when a value has been assigned to the
variable.
Types i, p, and f represent the three numeric types available. They differ in their inner representation
and their maximum value range and how it does the calculations. Over this and the next two slides
you will learn about the three types and look at their individual arithmetic in detail. This slide shows
you how the integer type i is defined and how it works. Type i is represented internally as a binary
number, works faster than the other two types and always rounds off the result of its calculation to the
next whole number.
The image on your screen shows you the valid operations for integers. If the result of the interim
calculation lies outside the maximum value range, then a runtime error occurs.
Floating point numbers are represented by binary precision floating point numbers. Floating point
numbers are normalized, and the exponent and the mantissa are also stored in the binary form.
The floating point operations of the relevant processors are used for calculations in floating point
arithmetic.
There may be inaccuracies as the algorithms are executed with binary numbers. It is recommended
that this data type is used when an approximation is allowed and not when accurate results are
expected.
It is possible to define an arithmetic expression of mixed data types including character types. The
character type must contain data that can be interpreted as numeric and there must be at least one
data object with a numeric type.
Once the arithmetic has been determined, all operands are converted to the numeric type that
corresponds to the chosen arithmetic. The system performs the calculation using these converted
values and ultimately converts the result to the desired result type. The image on your screen shows
you how this formula has been interpreted by the system.
From the image on your screen you can see that arithmetic to be used is decided by the combination
of all the types in the whole expression. Three rules are shown on the slide as an indicator of the
arithmetic that will be selected.
To avoid the problems of rounding off when using the type i and the inaccuracy when using the type
f, the type p was introduced.
This type has a wider range and includes decimal numbers. As can be seen from the image, the
length can be defined when defining the type. The term DECIMALS defines how many of the digits in
the length of type p are set aside for digits after the decimal point.
Packed numbers are the best option for business calculations where the correct rounding for the
specified number of decimal places is very important. The algorithm for this arithmetic is similar to
using a pencil and paper. Interim results initially use packed numbers with 31 decimal places. If an
overflow occurs, the entire expression is calculated again with an internal accuracy of 63 decimal
places. If another overflow occurs, an exception is raised and it can be handled.
Before the advent of the Unicode character set table, different character tables were used to
represent different sets and data interchange was not an easy process.
As of SAP Web Application Server 6.10 and later, the Unicode character set is also supported and
this allows for a possible 65,536 characters that could be represented.
The Unicode check needs to be activated for this to work and can be set for each program. If the
Unicode check is active, it can be run in a non Unicode supported system but the reverse is not true.
To summarize what you have looked at so far, you must remember that:
All elementary data objects defined as byte like data type will be treated as byte like data
objects.
All elementary data objects defined as character like data type will be treated as character like
data objects.
In non-Unicode systems, the byte like data objects is treated as character like data objects.
In Unicode systems, you must differentiate between them when defining them.
All flat structures are treated as character like data objects only if they contain character like
components.
In the image on your screen, you will see some statements. In each of these statements, the
operands are treated like type c fields, regardless of their actual field type. They are not converted.
In Unicode, the IN BYTE MODE will have to be added to ensure that each byte is examined
individually, and not in pairs. The corresponding IN CHARACTER MODE addition is optional for
processing character-like data objects.
The standard function STRLEN( ) is used to determine the occupied length of a string. The
corresponding function, XSTRLEN( ), is available for byte-like data objects.
As you can see from the image on your screen, when character fields are defined, the offset and
length are interpreted character by character. When defined as byte-like fields the values are
assumed in bytes.
In flat structures, offset access is possible even if they are not completely character-like as long as
the access does not go beyond the character-like area.
On your screen you can see that if we want to assign the value of one field to another, we can either
say MOVE source TO target or we can say target=source.
Both source and target should be of the same type. There is one more option shown on your screen.
The WRITE statement is used when you want to copy the source to the target in a formatted form. In
this case the target field has to be long enough to accommodate the formatting, for example, spaces
or separators.
There are two ways to declare structures: This can be done using the TYPES statement to explicitly
define a structure type to which the DATA statement refers, or construct the data type implicitly in the
DATA statement.
Sample code for both types is shown on your screen for reference.
Nested structures can be defined as deep as required. The components of the substructure can be addressed with
a corresponding chain of component names.
On your screen, you can see the sample code where the structure name is nested into the second structure
address.
A deep structure is a structure in which at least one component has a dynamic type, which can be an elementary
data object with variable length of type string or xstring, a reference to another data object, or an internal table.
The nested structure cannot be used when presenting data using the SAP List Viewer or in the Table
Control. The alternative is to use a Named Include. The sample code on your screen shows you how
this can be done. The structure s_name has three components to it, prename, surname and title.
The structure is called using the “Include Type … As.. “ statement. Notice how, it is no longer
represented as a nested structure and the three components are now called name.
To call a structure you can use the Include Structure statement.
The contents of one structure can be assigned to another structure. As mentioned earlier, the
program is more efficient if you can keep the types of the structures the same and avoid as much
automatic conversion as possible.
If the components of two structures are type compatible, but their component names are different,
these two structures are still compatibly typed as a whole.
To assign values between identically named structures, you can use the MOVE-CORRESPONDING
statement. The system will copy each source field to the target field matching it by name. On your
screen, the sample code shows you how the fields of name are moved into the matching fields in
address which has the fields of the same name among other fields. The interpretation of how this is
done is shown on the right of the screen.
You have already looked at predefined data types and how to work with them. In this topic, you will
learn about Global data types and constants, where they are created, and why and how to use them.
The Global data type is administered centrally and enables better use and control. As they reside in
the ABAP Dictionary, it is possible to use them in all your Repository objects.
Global data types are linked to business contexts based on content, and are actively integrated in the
SAP system. Therefore, when searching for a global data type, consider both technical and semantic
properties.
Global data types are centrally administered. Hence, you can use them in all repository objects. Also,
global data types minimize maintenance while maximizing system consistency.
Let us now look at what an ABAP Dictionary is.
The ABAP Dictionary manages global information that can be accessed and used throughout the
repository objects. This Dictionary includes the Search functions and the global data types.
Simple and complex structured data types can be defined in the ABAP Dictionary. You cannot
declare types or data objects that are known outside the boundaries of a program. Technical and
semantic information is also stored here.
The ABAP Dictionary also maintains all information about database tables which are addressed from
the SAP System. All changes to the database table is first completed in the ABAP Dictionary and
then copied to its appropriate place to ensure a consistent availability of information. This also
ensures that there is no dependency on platforms.
Programs in the SAP system may contain user dialogs to input and output data. Global types for
input and output fields have many useful attributes:
When a user enters invalid data, the value ranges that are defined for the Global data type are
automatically checked as they are globally available and an error message can go to the user. A
similar feature is available for formatting and semantic information. This is because the global data
objects have the same type as the columns in the database table.
The next advantage of the ABAP Dictionary lies in the fact that it not only maintains information about
the global data type but also contains all required information about the definitions of database table.
When you want to administer a database table, or you want to access data from the database table
form within your ABAP program, the fact that both these information are available globally ensures
ease of operations.
Global types are used to define the interface parameters for function modules and methods of global
classes. If this type is complete, you can use it directly, at runtime, to define the data object. This
makes your program more robust while reducing maintenance. On your screen you can see how the
ABAP program can directly call the function or the methods as they are linked to the global data type.
Now that we have seen why we need global data types and their advantages, let us look at the types
themselves in detail.
Inside the ABAP dictionary, the elementary data types are known as data elements. They can contain
technical and semantic information, field labels, details about F1 help and a link to the search help.
The image on your screen shows you how the Dictionary type is mapped to the ABAP type when
generating the program. After that, on actual execution of the program, it is the ABAP type that is
used. The documentation for the same is shown on the right of our image.
We have already seen that the global data types are formed from elementary types. Just like local
program data types they are technically identical to the predefined ABAP types. They are referred to
as predefined Dictionary types. Some examples of these types are shown in the table on your
screen. The two columns in blue show how the ABAP type is different from the Dictionary type.
Data elements are elementary Dictionary types. The options for assigning a technical property to a
data element are either by using a domain, or by direct type specification.
If you assign the technical properties using a domain, then the domain itself contains the technical
attributes. This reduces maintenance. It is recommended to use this method if the technical attributes
for usage have to be the same but not the semantic attributes.
With direct type specification, you would need to reference dictionary type and if this type is an
incomplete one, then the rest of the required information like length or decimal places has to be
defined for the data element.
The semantic information has to be defined for a data element.
Let us now look at how to use these data elements in a program. When you want to assign the type
to the input or output field using a data element, some existing attributes can be used.
With the predefined data type, the type and length attributes can be checked automatically and error
messages can be sent out automatically. Similarly, existing formatting or additional restrictions can
be used. Help information stored with a field is also available automatically when the user hits the F1
key. If any search help is assigned to the data element, which too is now accessible.
The image on your screen shows you how this is done, as the global data type is used for the
assignment. Let us now look at structures.
A structure contains several components and any number of them can be global types. The type to
each component can be assigned by using a data type, by direct type specification or by using a
structure type or a table type.
You have already seen the advantages of using the global data type. Similar advantages exist with
the use of direct type specification and using of the structure type enables you to use any level of
complexity.
The sample code on your screen shows you how to use Dictionary Structures in ABAP programs.
To define a screen interface, a flat global structure type can be used. Formatting options, online help
and search functions, validity checks and so on can come from the global types itself so that the
entire program is more efficient. Similarly, for the user, error messages can come from the global
types too, as these would have been defined there.
You have to define the structure for the data transport between screen and program with the TABLES
statement.
In continuation from the previous slide, here, you can see how all the various checks, like validity,
formatting options, semantic information can be accessed in the new structure as they are originating
from the global types. Similarly, technical information also comes from the technical type to your
ABAP program.
A type group has to be used to define a global constant. The name of this type group can contain up
to 5 characters.
The CONSTANTS statement is used to define constants in the type group.
All predefined ABAP types and the global Dictionary types can be used. To use the types of a type
group in a program, you specify the type group using the TYPE-POOLS statement.
The sample code on your screen shows you how this can be written.