Download - bc401-P-1-50
-
8/4/2019 bc401-P-1-50
1/50
BC401 ABAP Objects
BC401
Release 630 02.09.2003
-
8/4/2019 bc401-P-1-50
2/50
BC401 ABAP Objects.......................................................................................................................................................1
Copyright.......................................................................................................................................................................2
Course Prerequisites...................................................................................................................................................4
Target Group..............................................................................................................................................................5
Course Overview...........................................................................................................................................................1
Course Goal...............................................................................................................................................................2
Course Objectives......................................................................................................................................................3
Course Content (1).....................................................................................................................................................4
Course Content (2).....................................................................................................................................................5
Course Overview: Integration in Course Content......................................................................................................6
Main Business Scenario.............................................................................................................................................7
Technical Names........................................................................................................................................................8
Data Types and Data Objects in Detail..........................................................................................................................1
Data Types and Data Objects in Detail: Unit Objectives..........................................................................................2
Data Types and Data Objects in Detail: Integration in Course Content....................................................................3
Data Types and Data Objects in Detail (1)................................................................................................................4
Revision: Defining Data Objects...............................................................................................................................5
Predefined ABAP Types............................................................................................................................................6
Type Groups in the ABAP Dictionary.......................................................................................................................7
Data Types and Data Objects in Detail (2)................................................................................................................8
Integers and Integer Arithmetic.................................................................................................................................9
Packed Numbers and Fixed Point Arithmetic..........................................................................................................10
Floating Point Arithmetic........................................................................................................................................11
Summary: Recommendations for Using Numeric Data Types................................................................................12
Arithmetic and Mixed Expressions..........................................................................................................................13
Predefined ABAP Types for Character Strings.......................................................................................................14
Overview: Character String Processing...................................................................................................................15
Accessing Parts of Fields.........................................................................................................................................16
Data Types and Data Objects in Detail (3)..............................................................................................................17
Defining Structures with Local Types.....................................................................................................................18
Defining Nested Structures with Local Types.........................................................................................................19
Using Named Includes.............................................................................................................................................20
Data Types and Data Objects in Detail (4)..............................................................................................................21
Unicode: Overview..................................................................................................................................................22
Character-Type and Byte-Type Types.....................................................................................................................23
Functions and Comparison Operators......................................................................................................................24
Compatibility and Conversion.................................................................................................................................25
Conversion Under Unicode (1)................................................................................................................................26
Conversion Under Unicode (2)................................................................................................................................27
-
8/4/2019 bc401-P-1-50
3/50
Example of Using the Conversion Rules.................................................................................................................28
Offset and Length Accesses and Elementary Data Objects.....................................................................................29
Offset and Length Accesses and Structures.............................................................................................................30
Data Types and Data Objects in Detail: Summary..................................................................................................31
Data Types and Data Objects in Detail Exercise 1..................................................................................................32
Data Types and Data Objects in Detail Solution 1..................................................................................................34
Using Internal Tables.....................................................................................................................................................1
Using Internal Tables: Unit Objectives......................................................................................................................2
Using Internal Tables: Integration in Course Content...............................................................................................3
Using Internal Tables (1)...........................................................................................................................................4
The Internal Table - a Data Object of Variable Length.............................................................................................5
Attributes of Internal Tables......................................................................................................................................6
Link Between Table Kind and Access Type..............................................................................................................7
Using Internal Tables (2)...........................................................................................................................................8
Overview: Types of Definitions for Internal Tables..................................................................................................9The Data Type of an Internal Table.........................................................................................................................10
Table Attribute: Line Type......................................................................................................................................11
Table Attribute: Table Kind and Key......................................................................................................................12
Example: Standard Table with Local Type.............................................................................................................13
Example: Sorted and Hashed Table.........................................................................................................................14
The Standard Table Type.........................................................................................................................................15
Internal Tables with an Unstructured Line Type.....................................................................................................16
Using Internal Tables (3).........................................................................................................................................17
Overview: Types of Operation.................................................................................................................................18
Limiting the Scope of Line Operations: Target Records and Access Type.............................................................19
Preview: Field SymboIs (1).....................................................................................................................................20
Preview: Field SymboIs (2).....................................................................................................................................21
Insert........................................................................................................................................................................22
Reading a Single Record into a Work Area Using the Line Number......................................................................23
Reading a Single Record into a Work Area Using the Field Contents....................................................................24
Reading a Single Record Using a Field Symbol......................................................................................................25
Changing the Table Using a Work Area..................................................................................................................26
Changing a Single Record Using a Field Symbol...................................................................................................27
Delete.......................................................................................................................................................................28
Loop Processing.......................................................................................................................................................29
Overview of Operations...........................................................................................................................................30
Using Internal Tables (4).........................................................................................................................................31
Internal Tables with a Header Line..........................................................................................................................32
Selection Options and Selection Tables...................................................................................................................33
-
8/4/2019 bc401-P-1-50
4/50
-
8/4/2019 bc401-P-1-50
5/50
Introduction: Programming Models (1).....................................................................................................................5
Characteristics of Procedural Programming..............................................................................................................6
Typical ABAP Program.............................................................................................................................................7
ABAP Main Memory and Function Groups..............................................................................................................8
Function Group Example...........................................................................................................................................9
Use of Function Group.............................................................................................................................................10
Several Instances of One Function Group...............................................................................................................11
Instantiation and Object-Oriented Languages.........................................................................................................12
Introduction: Programming Models (2)...................................................................................................................13
Programming Models...............................................................................................................................................14
Characteristics of the Object-Oriented Approach....................................................................................................15
Technical Implementation of the Object-Oriented Approach..................................................................................16
Client/Server Behavior.............................................................................................................................................17
Compatibility and Design Aims...............................................................................................................................18
ABAP Main Memory and Encapsulation................................................................................................................19The Software Development Process........................................................................................................................20
Introduction to Object-Oriented Programming: Summary......................................................................................21
Analysis and Design......................................................................................................................................................1
Analysis and Design: Unit Objectives.......................................................................................................................2
Analysis and Design: Integration in Course Content.................................................................................................3
Analysis and Design (1).............................................................................................................................................4
Classification of Objects............................................................................................................................................5
Different Abstractions of a Class...............................................................................................................................6
Representation of a Class...........................................................................................................................................7
Classes and Objects....................................................................................................................................................8
Analysis and Design (2).............................................................................................................................................9
Modeling with UML................................................................................................................................................10
Example of a Class Diagram....................................................................................................................................11
Association...............................................................................................................................................................12
Aggregation and Composition.................................................................................................................................13
Generalization and Specialization............................................................................................................................14
Sequence Diagrams: Example.................................................................................................................................15
Sequence Diagram: Delegation................................................................................................................................16
Analysis and Design: Summary...............................................................................................................................17
Analysis and Design Exercises................................................................................................................................18
Analysis and Design Solutions................................................................................................................................19
Principles of Object-Oriented Programming.................................................................................................................1
Principles of Object-Oriented Programming: Unit Objectives..................................................................................2
Principles of Object-Oriented Programming: Integration in Course Content............................................................3
-
8/4/2019 bc401-P-1-50
6/50
-
8/4/2019 bc401-P-1-50
7/50
Principles of Object-Oriented Programming Solutions...........................................................................................48
Inheritance......................................................................................................................................................................1
Inheritance: Unit Objectives......................................................................................................................................2
Inheritance: Integration in Course Content................................................................................................................3
Inheritance: UML Example.......................................................................................................................................4
Multiple Inheritance?.................................................................................................................................................5
Relationships Between Superclasses and Subclasses................................................................................................6
Inheritance: Syntax....................................................................................................................................................7
Redefining Methods...................................................................................................................................................8
Inheritance and Redefining the Constructor..............................................................................................................9
Rules for Calling the Constructor............................................................................................................................10
Inheritance and Visibility.........................................................................................................................................11
Visibility of Protected Components.........................................................................................................................12
Rules for Redefining Methods.................................................................................................................................13
Redefining Methods: Example................................................................................................................................14Inheritance: Unit Summary......................................................................................................................................15
Inheritance Exercises...............................................................................................................................................16
Inheritance Solutions...............................................................................................................................................18
Casting...........................................................................................................................................................................1
Casting: Unit Objectives............................................................................................................................................2
Casting: Integration in Course Content......................................................................................................................3
Casting (1)..................................................................................................................................................................4
Principle of Narrowing Cast......................................................................................................................................5
Compatibility and Narrowing Cast............................................................................................................................6
Generic Access?.........................................................................................................................................................7
Preparations for Generic Access (1)..........................................................................................................................8
Preparations for Generic Access (2)..........................................................................................................................9
Polymorphism - Generic Access..............................................................................................................................10
Static and Dynamic Types of References................................................................................................................11
Comparison to Procedural Programming.................................................................................................................12
Casting (2)................................................................................................................................................................13
Use of Widening Cast..............................................................................................................................................14
Widening Cast: Example.........................................................................................................................................15
Casting (3)................................................................................................................................................................16
Semantics and Use with Inheritance........................................................................................................................17
Incorrect Use of Inheritance.....................................................................................................................................18
Casting: Unit Summary............................................................................................................................................19
Casting Exercises.....................................................................................................................................................20
Casting Solutions.....................................................................................................................................................22
-
8/4/2019 bc401-P-1-50
8/50
Interfaces........................................................................................................................................................................1
Interfaces: Unit Objectives........................................................................................................................................2
Interfaces: Integration in Course Content..................................................................................................................3
Interfaces (1)..............................................................................................................................................................4
Interfaces: Use...........................................................................................................................................................5
Interfaces: Services....................................................................................................................................................6
Interface: UML Example...........................................................................................................................................7
Defining and Implementing an Interface...................................................................................................................8
Interfaces: Features....................................................................................................................................................9
Interfaces (2)............................................................................................................................................................10
Working with Interface Components.......................................................................................................................11
Interface References: Narrowing Cast.....................................................................................................................12
Using the Interface...................................................................................................................................................13
Polymorphism and Interfaces..................................................................................................................................14
Interface References: Widening Cast.......................................................................................................................15Interfaces (3)............................................................................................................................................................16
Compound Interfaces...............................................................................................................................................17
Compound Interfaces: Example...............................................................................................................................18
Interfaces: Unit Summary........................................................................................................................................19
Interfaces Exercises.................................................................................................................................................20
Interfaces Solutions..................................................................................................................................................25
Events.............................................................................................................................................................................1
Events: Unit Objectives.............................................................................................................................................2
Events: Integration in Course Content.......................................................................................................................3
Events: Overview.......................................................................................................................................................4
Features......................................................................................................................................................................5
Triggering and Handling Events: Overview..............................................................................................................6
Defining and Triggering Events: Syntax...................................................................................................................7
Handling and Registering Events...............................................................................................................................8
Event Handler Methods.............................................................................................................................................9
Registering for an Event: Syntax.............................................................................................................................10
Registration/Deregistration: Handler Tables...........................................................................................................11
Event Handling: Features.........................................................................................................................................12
Events: Unit Summary.............................................................................................................................................13
Events Exercises......................................................................................................................................................14
Solutions..................................................................................................................................................................16
Global Classes and Interfaces........................................................................................................................................1
Global Classes and Interfaces: Unit Objectives.........................................................................................................2
Global Classes and Interfaces: Integration in Course Content..................................................................................3
-
8/4/2019 bc401-P-1-50
9/50
Global Classes and Interfaces (1)..............................................................................................................................4
Review: Local Classes and Interfaces........................................................................................................................5
Global Classes and Interfaces....................................................................................................................................6
Creating Global Classes in the Object Navigator............. .......... ........... .......... ........... .......... ........... .......... ........... ....7
Class Builder: Attributes............................................................................................................................................8
Class Builder: Methods..............................................................................................................................................9
Class Builder: Interface Methods.............................................................................................................................10
Class Builder: Method Interface..............................................................................................................................11
Class Builder: Method Implementation...................................................................................................................12
Class Builder: Inheritance........................................................................................................................................13
Class Builder: Test Environment.............................................................................................................................14
Global Classes and Interfaces (2)............................................................................................................................15
Global Classes in the Object Navigator...................................................................................................................16
Class Builder: CREATE OBJECT...........................................................................................................................17
Class Builder: CALL METHOD.............................................................................................................................18Global Classes and Interfaces (3)............................................................................................................................19
SAP Grid Control.....................................................................................................................................................20
Including a Grid Control Instance in a Dialog Program..........................................................................................21
Displaying Data with a Grid Control Instance.........................................................................................................22
Global Classes and Interfaces: Unit Summary........................................................................................................23
Exercises..................................................................................................................................................................24
Solutions..................................................................................................................................................................26
Special Techniques........................................................................................................................................................1
Special Techniques: Unit Objectives.........................................................................................................................2
Special Techniques: Integration in Course Content...................................................................................................3
Special Techniques (1)...............................................................................................................................................4
Abstract Classes.........................................................................................................................................................5
Final Classes..............................................................................................................................................................6
Special Techniques (2)...............................................................................................................................................7
Who Can Instantiate Classes?....................................................................................................................................8
Special Techniques (3)...............................................................................................................................................9
Friends......................................................................................................................................................................10
Special Techniques (4).............................................................................................................................................11
Persistence Service...................................................................................................................................................12
Features of the Persistence Service..........................................................................................................................13
Class Agent..............................................................................................................................................................14
Special Techniques: Unit Summary.........................................................................................................................16
Exercises - optional..................................................................................................................................................17
Solutions - optional..................................................................................................................................................20
-
8/4/2019 bc401-P-1-50
10/50
Exception Handling.......................................................................................................................................................1
Exception Handling: Unit Objectives........................................................................................................................2
Exception Handling: Integration in Course Content..................................................................................................3
Class-Based Exceptions: Overview...........................................................................................................................4
Exception Classes: The Inheritance Hierarchy..........................................................................................................5
Handling Exceptions..................................................................................................................................................6
Example: Handling a Predefined Exception..............................................................................................................7
Example: Using Your Own Exceptions.....................................................................................................................8
Creating Your Own Exception Classes......................................................................................................................9
Maintaining Your Own Attributes and Exception Texts.........................................................................................10
Raising and Handling Exceptions You Have Written.............................................................................................11
Passing Exceptions Along........................................................................................................................................12
Example: Passing Exceptions Along.......................................................................................................................13
Exceptions That Must Be Declared.........................................................................................................................14
Exception Handling: Unit Summary........................................................................................................................15Exception Handling Exercise 1................................................................................................................................16
Exception Handling Solution 1................................................................................................................................18
Dynamic Programming..................................................................................................................................................1
Dynamic Programming: Unit Objectives...................................................................................................................2
Dynamic Programming: Integration in Course Content............................................................................................3
Dynamic Programming (1)........................................................................................................................................4
Field Symbols............................................................................................................................................................5
The CASTING Addition............................................................................................................................................7
Dynamic Programming (2)........................................................................................................................................9
Using Dynamic Attributes in Statements.................................................................................................................10
Calling Methods Dynamically.................................................................................................................................11
Dynamic Programming (3)......................................................................................................................................12
Data Reference Variables........................................................................................................................................13
Example: Using Data Reference Variables in Dynamic Method Calls...................................................................14
Generating Data Objects at Runtime.......................................................................................................................15
Dereferencing Data References...............................................................................................................................16
Example: Generating Data Objects at Runtime.......................................................................................................17
Dynamic Programming (4)......................................................................................................................................18
Ascertaining Internal Table Attributes.....................................................................................................................19
Dynamic Type Descriptions for RTTI Classes........................................................................................................20
Declaring Types Dynamically: Example.................................................................................................................21
Dynamic Programming: Unit Summary..................................................................................................................22
Exercise 1 Optional...............................................................................................................................................23
Solution 1 - Optional................................................................................................................................................26
-
8/4/2019 bc401-P-1-50
11/50
0
SAP AG 2002
BC401 ABAP Objects
BC401BC401
ABAP ObjectsABAP Objects
System: SAP R/3
System requirements: SAP R/3, Basis Release 6.10 or later
2002/Q3
Material number: 50054667
-
8/4/2019 bc401-P-1-50
12/50
0.2
SAP AG 2002
Copyright 2002 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted inany form or for any purpose without the express permission ofSAP AG. The information contained herein may be changedwithout prior notice.
Copyright
Notes on Trademarks:
Some software products marketed by SAP AG and its distributors contain proprietary software
components of other software vendors.
Microsoft, WINDOWS, NT, EXCEL, Word, PowerPoint, and SQL Server are registered
trademarks of Microsoft Corporation.
IBM, DB2, OS/2, DB2/6000, Parallel Sysplex, MVS/ESA, RS/6000, AIX, S/390, AS/400,
OS/390, and OS/400 are registered trademarks of IBM Corporation.
ORACLE is a registered trademark of ORACLE Corporation.
INFORMIX-OnLine for SAP and INFORMIX Dynamic ServerTM are registered trademarks
of Informix Software Incorporated.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. HTML, DHTML, XML, and XHTML are trademarks or registered trademarks of W3C, World Wide
Web Consortium, Massachusetts Institute of Technology.
JAVA is a registered trademark of Sun Microsystems, Inc.
JAVASCRIPT is a registered trademark of Sun Microsystems, Inc., used under license for technology
developed and implemented by Netscape.
SAP, SAP Logo, R/2, RIVA, R/3, ABAP, SAP ArchiveLink, SAP Business Workflow, WebFlow, SAP
EarlyWatch, BAPI, SAPPHIRE, Management Cockpit, mySAP.com Logo, and mySAP.com are
trademarks or registered trademarks of SAP AG in Germany and several other countries all over the
-
8/4/2019 bc401-P-1-50
13/50
world. All other products mentioned are trademarks or registered trademarks of their respective
companies.
-
8/4/2019 bc401-P-1-50
14/50
0.3
SAP AG 2002
Course Prerequisites
Programming experience
SAPTEC(mySAP Technology Solution Overview)
BC400(ABAP Workbench: Foundations and Concepts)
Programming experience in ABAP
-
8/4/2019 bc401-P-1-50
15/50
0.4
SAP AG 2002
Participants: Developers, consultants
Duration: 5 days
Target Group
Notes to the user
The training materials are not appropriate for self-study. They complement the course instructor's
explanations. There is space for you to write down additional information on the sheets.
There may not be enough time during the course to complete all the exercises. The exercises are
intended as additional examples of the topics discussed during the course. Participants can also use them
as an aid to enhancing their knowledge after the course has finished.
-
8/4/2019 bc401-P-1-50
16/50
-
8/4/2019 bc401-P-1-50
17/50
1
SAP AG 2002
Course goal
Course objectives
Course content
Course overview diagram
Main business scenario
Contents:
Course Overview
(C) SAP AG BC401 1
-
8/4/2019 bc401-P-1-50
18/50
1.2
SAP AG 2002
This course will enable you to:
Learn the principles of object-orientedprogramming
Learn the structure and application ofABAP Objects
Course Goal
(C) SAP AG BC401 2
-
8/4/2019 bc401-P-1-50
19/50
1.3
SAP AG 2002
At the conclusion of this course, you will be able to:
Describe and use the most important principles:
Internal tables
Classes/inheritance/interfaces
Polymorphism, inheritance, and interfaces
Events
Exception handling
Dynamic programming
Develop programs in ABAP Objects
Course Objectives
(C) SAP AG BC401 3
-
8/4/2019 bc401-P-1-50
20/50
1.4
SAP AG 2002
Unit 1 Course Overview
Unit 2 Data Types and Data Objects in Detail
Unit 3 Using Internal Tables
Unit 4 Calling Programs and Passing Data
Unit 5 Introduction to Object-Oriented Programming
Unit 6Analysis and Design
Unit 7 Principles of Object-Oriented Programming
Unit 8 Inheritance
Unit 9 Casting
Unit 10 Interfaces
Preface
Course Content (1)
(C) SAP AG BC401 4
-
8/4/2019 bc401-P-1-50
21/50
1.5
SAP AG 2002
Unit 11 Events
Unit 12 Global Classes and Interfaces
Unit 13 Special Techniques
Unit 14 Exception Handling
Unit 15 Dynamic Programming
Appendix
Course Content (2)
(C) SAP AG BC401 5
-
8/4/2019 bc401-P-1-50
22/50
1.6
SAP AG 2002
Course Overview: Integration in Course Content
CourseOverview
CourseOverview
(C) SAP AG BC401 6
-
8/4/2019 bc401-P-1-50
23/50
1.7
SAP AG 2002
A travel agent maintains its connections tobusiness partners - such as airlines, car hirecompanies, and hotels
Main Business Scenario
(C) SAP AG BC401 7
-
8/4/2019 bc401-P-1-50
24/50
1.8
SAP AG 2002
Technical Names
Package: BC401
Naming conventions for program objects:
Demonstrations: SAPBC401_xxxD_...
Copy templates: SAPBC401_xxxT_...
Model solutions: SAPBC401_xxxS_...
xxx is the individual unit code
Unit codes:
Unit 2: DTO
Unit 3: TAB
Unit 4: CAL
Unit 7: AIR
Unit 8: INH
Unit 9: CAS
Unit 10: INT
Unit 11: EVE
Unit 12: CLS
Unit 13: SPCUnit 14: EXC
Unit 15: DYN
(C) SAP AG BC401 8
-
8/4/2019 bc401-P-1-50
25/50
2
SAP AG 2002
Data objects in programs
Elementary data objects
Structures
Character string processing in Unicode
Contents:
Data Types and Data Objects in Detail
(C) SAP AG BC401 1
-
8/4/2019 bc401-P-1-50
26/50
-
8/4/2019 bc401-P-1-50
27/50
2.3
SAP AG 2002
Data Types and Data Objects in Detail:Integration in Course Content
DataTypesand
DataObjectsin
Detail
DataTypesand
DataObjectsin
Detail CourseOverview
(C) SAP AG BC401 3
-
8/4/2019 bc401-P-1-50
28/50
2.4
SAP AG 2002
Special Features in UnicodeSpecial Features in Unicode
Elementary Data ObjectsElementary Data Objects
StructuresStructures
Data Types and Data Objects in Detail (1)
IntroductionIntroduction
(C) SAP AG BC401 4
-
8/4/2019 bc401-P-1-50
29/50
2.5
SAP AG 2002
Revision: Defining Data Objects
PredefinedABAP types
x
Local types
i
f
string
xstring
t
d
Globaltypes
c
n
p
TYPES type_name TYPE ...
DATA do_name TYPE type_name.DATADATA
DATA do_name_new LIKE do_name.DATADATA
Data objects are usually defined with the DATAstatement as follows. After the name of the data object, a
a fully-specified type is assigned to it using the TYPE addition.The type is linked to the data object
statically and cannot be changed at runtime.
There are other syntax variants available (for historical reasons).Note however, that some of these
historical variants are no longer supported in ABAP Objects.For further information, refer to the
keyword documentation for theDATAstatement.
All types are based on predefined ABAP types, which will be discussed in greater detail in the following
slide.Some of these ABAP types are fully specified and can be used to type data objects directly. Other
types need to be modified to include the length, and in some cases the number of decimal places so that
they are fully specified. These are the simplest examples of user-defined types.
You can create complex structured types of any level of complexity, based on elementary types.
You can also define types centrally in theABAP Dictionary. You can then use these global types in all
appropriate Repository objects in the system.
(C) SAP AG BC401 5
-
8/4/2019 bc401-P-1-50
30/50
2.6
SAP AG 2002
string
Predefined ABAP Types
p
i
f
n
c
d
t
x
xstring
Data
typeNumeric
4
8
1 .. 16
Integer
Float. point number
Packed number
Character string type / hexadecimal
Fixedlength
Description Length in bytes
Number sequence
Character sequence
Date
Time
Hexadecimal code
Character sequence
Hexadecimal code
1 .. 65535
1 .. 65535
8
6
1 .. 65535
Differ in:
Rules for storage
Value range
Arithmetic used
Bit operations
Character string type
Hexadecimal
Runtime system adjusts lengthdynamically
Character string operations
(allowed for all types)
+ date calculations
+ time calculations
Attributes
Variable
length
The following ABAP types are fully specified and can be used directly to type data objects:d, t, i, f,
string, and xstring. A special feature of the string and xstring types is that their length is
adjusted dynamically at runtime.Thus, for these two types, length is not a component of the type.
The following ABAP types are not fully specified, so you must include additional information before
you use them to define data objects:
c, n, andx
Length needs to be added. Permitted values: 1 to 65535 characters.
If you do not declare a length when you define a data object, the system assigns the default length of
1.p
You must extend the type to include the length, and number of decimal places, Permitted length: 1 to
16 bytes. If you do not declare a length when you define a data object, the system assigns the default
length of8 bytes (that is, 15 digits) and 0 decimal places.
The type also contains information on how the data is stored, what range of values is allowed, what
operations can be performed on the data, and what sort of arithmetic is used (if the data object is suitable
for use in calculations). We will deal with this topic in greater detail later in this unit.
(C) SAP AG BC401 6
-
8/4/2019 bc401-P-1-50
31/50
2.7
SAP AG 2002
Type Groups in the ABAP Dictionary
Type group zmytp
TYPE-POOL zmytp.
CONSTANTS zmytp_const_name ...
[ TYPES zmytp_type_name1 ... ]
[ TYPES zmytp_type_name2 ... ]
...
Using a constant:
TYPE-POOLS: zmytp.
[ DATA var TYPE zmytp_type1. ]
IF var = zmytp_const_name.
...
ENDIF.
zmytpzmytp
zmytpzmytp
zmytpzmytp
zmytpzmytp
zmytpzmytp
zmytpzmytp
Name of type group:
Up to 5 characters;each type name and
constant namestarts with the name of
the type group.
TYPETYPE--POOLSPOOLS
Each type group, whosetypes and constants you
want to use, must be specifiedin the ABAP program
ABAP program
zmytpzmytp
You must use a type group to define global constants.The name of the type group can contain up to 5
characters.
You define constants in the type group using the CONSTANTS statement, just as in an ABAP program.
You only have to adhere to the following namespace convention:
All constants (and type names) must have the name of the type group as a prefix.
You can use either a global Dictionary type or a predefined ABAP type as a valid type.
To be able to use the types of a type group in a program, you must refer to the type group using the
TYPE-POOLS statement.
After this line in the program, you can then use all the constants in the type group.
You can also define global data types in a type group. Before SAP R/3 Basis Release 4.5, this was the
only way to define complex global data types.
(C) SAP AG BC401 7
-
8/4/2019 bc401-P-1-50
32/50
2.8
SAP AG 2002
Data Types and Data Objects in Detail (2)
Special Features in UnicodeSpecial Features in Unicode
Elementary Data ObjectsElementary Data Objects
StructuresStructures
IntroductionIntroduction
(C) SAP AG BC401 8
-
8/4/2019 bc401-P-1-50
33/50
2.9
SAP AG 2002
Integer auxiliary variable
1
int2 = 4 int3 = 3 int4 = 2
24 3 += /
2+ 3= =
int1
Integers and Integer Arithmetic
+
-
*
/
DIV
MOD
**
AdditionSubtractionMultiplicationDivisionInteger divisionRemainder integer div.Power
Arithmetic operations forintegers:
int1 = int2 / int3 + int4.
DATA: counter TYPE i VALUE 1. Length: 4 bytes
Value range [-2147483648; 2147483647]
Integer Arithmetic
All internal auxiliary fields are integers
Interim results are rounded
In integer arithmetic, the system always rounds to the appropriate decimal place. So, for example:DATA int TYPE i. int = 4 / 10. " result: 0
int = 5 / 10. " result: 1
Calculations performed using integer arithmetic are faster than calculations using fixed point or floating
point arithmetic.
For further information on calculations performed on integers, refer to the keyword documentation for
the COMPUTE statement.
(C) SAP AG BC401 9
-
8/4/2019 bc401-P-1-50
34/50
2.10
SAP AG 2002
Packed Numbers and Fixed Point Arithmetic
0 1 2 3 4 +
1 digit per
half-byteSign
DATA: percentage(3) TYPE p DECIMALS 2 VALUE '12.34'.(3)(3) DECIMALS 2DECIMALS 2
Length in bytes:n bytes correspond to
2n - 1 digits
Number of decimalplaces
12,34Number
Fixed point arithmetic:"like using paper and pencil"
Permitted length: 1 to 16 bytes (= 1 to 31 digits)
Number of decimal places < number of digits, max. 15
Example: Length 3 bytes, 2 decimal places Value range: [-999.99,+999.99]
Internal representation
1234,56+ 78,5
1313,06
The length of packed numbers is given in bytes. The connection between value range and length is
derived from the internal representation: Each decimal digit is represented by a half-byte. The last byte is
reserved for the plus or minus sign.
The number of decimal places is defined using the DECIMALS addition. The maximum number of
decimal places is either 15 or the length of the number minus 1 (that is, there must be at least one digit
before the comma).
In user dialogs, decimal numbers are formatted according to the settings in the user master record.
Decimal point-aligned fixed point arithmetic is used as default for calculations. Packed numbers are thus
well-suited to 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 "pencil and paper".
The system always uses packed numbers of maximum length for interim results. You can switch off fixed point arithmetic in the program attributes. If you do, the DECIMALSaddition
when defining a packed number only affects the output of the number. Internally, all numbers are
interpreted as integers, regardless of the position of the decimal point. The fixed point arithmetic option
is always selected by default. You should always accept this value and use packed numbers for business
calculations.
(C) SAP AG BC401 10
-
8/4/2019 bc401-P-1-50
35/50
2.11
SAP AG 2002
1499023437,0
1494140625,0
1484375,0
140625,0
125,0
2048
1
1024
1
128
1
64
181
212121212115,0
2
1
21215,1
1110763
10
1
=
+=
+=
+=
+=
+++++=
+++++=
+=
+=
-----
-
Floating Point Arithmetic
| |
| | | | |0 0 0 0 . . .
Only 53 bits available
Only forapproximations
Unlike packed numbers, floating point numbers are represented using sums of binary fractions. Floating
point numbers are also normalized, and both the exponent and the mantissa are stored in binary form.
This representation complies with the IEEE norm for double precision floating point numbers. The
floating point operations of the relevant processors are used for calculations.
Since algorithms are converted to binary, inaccuracies can occur. Therefore, floating point numbers are
not suitable for business calculations.
Example:
You want to calculate 7.72% of 73050 and display the result accurate to two decimal places. The answer
should be 5310.74 (73050 * 0.0727 = 5310.7735). The program, however:DATA: float TYPE f, pack TYPE p DECIMALS 2.
float = 73050 * '0.0727'. " result: 5.3107349999999997E+03pack = float. WRITE pack. " result: 5310.73
You should therefore only use floating point numbers for approximations. When you compare
numbers, always use intervals, and always round at the end of your calculations.
The advantage of floating point numbers is the large value range: It comprises numbers from is, from
2,2250738585072014E-308 to 1,7976931348623157E+308 including both positive and negative
numbers as well as zero.In addition, you must use floating point numbers for special aggregation
functions of the SELECT statement.
(C) SAP AG BC401 11
-
8/4/2019 bc401-P-1-50
36/50
2.12
SAP AG 2002
Summary: Recommendations for Using NumericData Types
Required:Required: Recommended predefined ABAP data type:Recommended predefined ABAP data type:
Integers onlyType i,
since calculations using integer arithmetic arefastest
Decimal numbers forbusiness calculations Type p
Decimal numbers forrough calculationsperformed on very smallor very large numbers
Type F
Note:
The results of the following functions have the data type f:
Trigonometric functions:cos, acos,sin, asin, tan, atan
Hyperbolic functions: tanh, sinh, cosh
Exponential functions (base e): exp
Natural logarithms (base e): log
Logarithms (base 10): log10
Square root:sqrt
(C) SAP AG BC401 12
-
8/4/2019 bc401-P-1-50
37/50
2.13
SAP AG 2002
Arithmetic and Mixed Expressions
ca br = / +
p DECIMALS 3 i i f
0.0000...E+000201 200/ +
Float. point arithmetic
Conversion after f
and calculation
0.0000...E+0001.004999...E+000
Interim result:
+ = 1.004999...E+000
Conversion afterp DECIMALS 3
1.005
Fixed point arithmetic is the default
Integer arithmetic is only used if allthe components are integers
Floating point arithmetic is used if at least onecomponent is a floating point number
All components are converted to the relevant data type
After the calculation, all are converted to the result type
Example:
An arithmetic expression may contain any data types that are convertible into each other and into the
type of the result field.
The system converts all the values into one of the three numeric data types (i,p, or f), depending on the
data types of the operands. The ABAP runtime system contains an arithmetic for each of the three data
types. The system then performs the calculation and converts it into the data type of the result field.
This may mean that the same arithmetic expression leads to different results when performed on
different combinations of data types.
It is also possible for an arithmetic expression to have only character string type data objects, as long
as their contents are appropriate. The values are converted to numeric type objects. Bear in mind that
conversions affect performance. Wherever possible, choose a data type that does not require runtime
conversion. If an arithmetic expression contains numeric literals, the choice of arithmetic depends on the size of the
number: If the number is within the value range for the data type i, the numeric literal is interpreted as
an integer. If the value of the literal is greater than 2147483647, it is interpreted as a packed number.
Example:DATA int TYPE i. int = 1000000000 / 300000000 * 3. "result: 9
int = 10000000000 / 3000000000 * 3. "result: 10
(C) SAP AG BC401 13
-
8/4/2019 bc401-P-1-50
38/50
2.14
SAP AG 2002
Predefined ABAP Types for Character Strings
Description
Length
Value range
Calculations
Formattingoptions
1 ..1 .. 6553565535
characterscharacters1 .. 65535characters
8 digitsYYYYMMDD
6 digitsHHMMSS
Depends onDepends on codepagecodepage
By GregoriancalendarBy clock
TypeType ccType nType dType t
Time DateSequenceof digits
FixedFixed--lengthlength
char. stringchar. string
Datearithmetic
Timearithmetic
Based onuser defaultvalues
HH:MM:SS
Digits
VariableVariable
TypeType stringstring
Char. stringChar. string
of variableof variable
lengthlength
Conversion Conversion ConversionConversion
The value range of each string depends on the code page, which contains all the supported characters in
form of a table. Internally, each character is represented by a code number. When the system outputs the
character, it uses the code page to convert this number. To find the code page valid in a given system,
choose Tools CCMS Spool Administration Full Administration Character Sets.
The initial value of each character string with fixed length is a space character.
Numeric strings are represented internally as character strings. Note, however, that only digits are
permissible as characters. When character strings are assigned to numeric strings, the system ignores
letters and only copies the digits (right-aligned). Missing characters are filled with zeros.
The initial value of each character in a numeric string is a zero.
Only sequences of digits are valid for values of type d. These digits form a meaningful date, which
complies with the Gregorian calendar. The first four digits represent the year, the next two the monthand the last two the date. For performance reasons however, the object is only checked if it is an input
field on a screen or selection screen.
The initial value of a date is '000000'. The formatting options are determined by the user settings.
For values of type t, a sequence of digits is only valid if it can be interpreted as a time on the 24-hour
clock. The rules for interpreting the value are analogous to those used for dates.
The initial value of a time is '000000'.
(C) SAP AG BC401 14
-
8/4/2019 bc401-P-1-50
39/50
-
8/4/2019 bc401-P-1-50
40/50
2.16
SAP AG 2002
Accessing Parts of Fields
+() ...
REPORT ...
PARAMETERS:
pa_str(40) LOWER CASE,
pa_pos TYPE i,
pa_len TYPE i.
WRITE pa_str+pa_pos(pa_len). 19991231ttA235959
000000
Possible with anycharacter-type field
++papa__pospos((papa__lenlen))
In any statement that operates on a character-type field, you can address part of the field or structure by
specifying a starting position and a number of characters. If the field lengths are different, the system
either truncates the target or fills it with initial values. The source and target fields must have the type x,
c, n, d, t, or STRING.You can also use structures.
Example
MOVE +() TO +().
This statements assigns characters of field starting at offset to
characters of starting at offset .
(C) SAP AG BC401 16
-
8/4/2019 bc401-P-1-50
41/50
2.17
SAP AG 2002
Special Features in UnicodeSpecial Features in Unicode
Elementary Data ObjectsElementary Data Objects
StructuresStructures
Data Types and Data Objects in Detail (3)
IntroductionIntroduction
(C) SAP AG BC401 17
-
8/4/2019 bc401-P-1-50
42/50
2.18
SAP AG 2002
Defining Structures with Local Types
TYPES:
BEGIN OF s_name_type,
prename(25) TYPE c,
surname(25) TYPE c,
title(5) TYPE c,
END OF s_name_type.
DATA:
s_name TYPE s_name_type .
DATA:
BEGIN OF s_name,
prename(25) TYPE c,
surname(25) TYPE c,
title(5) TYPE c,
END OF s_name.
DATA:DATA:
TYPES:TYPES:
BEGIN OFBEGIN OF
END OFEND OF
BEGIN OFBEGIN OF
END OFEND OF
DATA:DATA:
START-OF-SELECTION.
s_name-prename = 'Smith'.
s_name-surname = 'John'.
s_name
s_name
s_name
s_name
Alternatives
s_name_type
s_name_type
s_name-
-
Definition of a structure type
Definition of a structure withreference to a user-defined type
Definition of a structure withimplicit type construction
As with elementary data objects, you can define structures in two ways:
First, define a structure type explicitly using the TYPES statement.
To do this, enter the name of the structure after BEGIN OF and then list the definitions of all the
components. End the definition of the structure type using END OF.
You then define the structured data object with the DATAstatement, using your own user-defined
structure type.
Define the data object directly using the DATAstatement. The syntax is similar to the definition of a
structure type. If you use this option, the corresponding structure type is defined implicitly at the same
time.
In both cases, the type is defined locally. Bear in mind that you can also use globally defined types
instead.
You address components of structures using:structure_name-comp_name.
For this reason, you should avoid using hyphens in variable names.
(C) SAP AG BC401 18
-
8/4/2019 bc401-P-1-50
43/50
2.19
SAP AG 2002
Defining Nested Structures with Local Types
Nested structureNested structure
Structure typeStructure type
name
streetcity
prenamesurname
title
s_address
s_name_typeprenamesurnametitle
TYPES:
BEGIN OF s_name_type,
prename(25) TYPE c,
surname(25) TYPE c,
title(5) TYPE c,
END OF s_name_type.
DATA:
BEGIN OF s_address,
name TYPE s_name_type,
street(15) TYPE c,
city(25) TYPE c,
END OF s_address.
s_address-name-surname = 'Smith'.
s_address-city = 'London'.
s_name_type
-- --
--
s_name_type
Application:Logical subgrouping
of data
You can define nested structures by assigning a structure type to a component within a structure type.
You can address this substructure as a whole using the component name:structure_name-substructure_name.
You can also address individual components in the substructure:structure_name-substructure_name-comp_name.
Structures can be nested to any level you wish.
You can also integrate components of a dynamic type in a structure. This can either be an elementary
data object of variable length (string or xstring),an internal table, or a reference. These structures
are known as deep structures.
There are constraints on how such deep structures can be used. For instance, a deep structure cannot be
used as a whole in theINTO clause of the SELECT statement. (Instead, each component must be listedseparately). Offset operations are also not appropriate. For more information, refer to SAP Note 176336.
(C) SAP AG BC401 19
-
8/4/2019 bc401-P-1-50
44/50
2.20
SAP AG 2002
Using Named Includes
DATA:
BEGIN OF t_linetype,
prename(25) TYPE c,
surname(25) TYPE c,
title(5) TYPE c,
END OF s_name_type.
DATA
BEGIN OF s_address.
INCLUDE STRUCTURE s_name_type
AS name.
DATA:
street(15) TYPE c,
city(25) TYPE c,
END OF s_address.
s_address-name-surname = 'Smith'.
* or:
s_address-surname = 'Smith'.
s_name_type
s_name_typeINCLUDE STRUCTUREINCLUDE STRUCTUREASAS
Structure with named includeStructure with named include
Structure typeStructure type
streetcity
prenamesurnametitle
s_address
s_name_typeprenamesurnametitle
name
-- --
--Use:
Logical subgroup desired, but nestedstructures not technically possible
In some cases, you cannot use nested structures, for example:
Because you can only define database tables with flat line types
In situations where user dialogs with a table-type display can only contain simple structures (such as in
the SAP List Viewer or Table Control)
In situations like these, you may still want to group parts of the structure and address it as a whole. You
do this using named includes:
You can include a substructure in another structure, and give this substructure a name:DATA: BEGIN OF structure_name,
... .
INCLUDE STRUCTURE substructure_type AS name.
DATA: ... ,END OF structure_name.
You can address this substructure using structure_name-name. You can address the fields in the
substructure using structure_name-name-comp_nameor directly using structure_name-
comp_name. Technically, however, this structure is not nested.
If naming conflicts occur - for example, if you want to include the same substructure twice - you can
append another name to the component names using RENAMING. For further information, refer to the
keyword documentation for the INCLUDE STRUCTURE statement.
(C) SAP AG BC401 20
-
8/4/2019 bc401-P-1-50
45/50
2.21
SAP AG 2002
Special Features in UnicodeSpecial Features in Unicode
Elementary Data ObjectsElementary Data Objects
StructuresStructures
Data Types and Data Objects in Detail (4)
IntroductionIntroduction
(C) SAP AG BC401 21
-
8/4/2019 bc401-P-1-50
46/50
2.22
SAP AG 2002
Unicode: Overview
Solution: Unicode
ONE codepage thatcomprises all known
characters
Every character is generally
displayed by two bytes
a
S
aUnicodeUnicode
Previous problems
Mixture of different incompatible character sets in one central
system
Data exchange between systems with incompatible character sets
To be able to work with Unicode, you must have a Unicode-compatible SAP System installed that itself
has a corresponding operating system and database. The ABAP programs must also be Unicode-
compatible.
In Unicode programs, other syntax rules apply than in non-Unicode programs. This is due to the
difference between the length in bytes and the number of characters in a character set in Unicode.
Existing programs are affected by a conversion to Unicode if an explicit or implicit assumption is made
about the internal length of a character. To execute the relevant syntax checks, you must checkUnicode
Checks Active under program attributes.
In a Unicode system, you can only execute programs that have the Unicode flag set. If the Unicode flag
is set for a program, the syntax check and program are executed in accordance with the rules described
in the Unicode online help (irrespective of whether it is a Unicode or a non-Unicode system). If the Unicode flag is not set, the program can only be executed in a non-Unicode system. For such
programs, the Unicode-specific changes of syntax and semantics do not apply. However, you can use all
language enhancements introduced in connection with the conversion to Unicode.
(C) SAP AG BC401 22
-
8/4/2019 bc401-P-1-50
47/50
-
8/4/2019 bc401-P-1-50
48/50
2.24
SAP AG 2002
Functions and Comparison Operators
Functions
Character-type type
XSTRLEN
Byte-type type
Comparisonoperators
Functions STRLEN
CO, CA, CS, CP,
CN, NA, NS, NP
BYTE-CO, BYTE-CA,
BYTE-CS, BYTE-CN,
BYTE-NA, BYTE-NS
STRLEN
DATA: c_fld1(4) TYPE c VALUE 'HUGO',
c_fld2(1) TYPE c VALUE 'G'.
IF c_fld1 CA c_fld2.
...
ENDIF.
DATA: x_field(4) TYPE x
VALUE 'E391B9A2',
len TYPE i.
len = XSTRLEN( x_field ).
Length in
bytes
Examples:
Length in
characters
The operators CO, CN, CA,NA, CS,NS, CP,NP are available for comparing the contents of character
string type data objects (for syntax and semantics, see keyword documentation). As with the statements
for string processing, these operators require single fields of type c, n, d, t, or stringas arguments.
Again, character-type structures are also permitted.
The Xvariants of the string comparison operators are distinguished from the character string variants by
the BYTE- prefix. For this operation, only X fields and fields of the type xstring are allowed as
arguments.
The STRLENfunction only works with character-type fields and returns the length in characters. With c
fields, only the so-called occupied length is relevant, that is, unlike with strings, trailing blanks are not
counted.
The XSTRLEN function is available for the length of byte sequences. For X strings, XSTRLENreturns
the current length and for Xfields, the defined length in bytes, where null bytes at the end of fields are
counted.
(C) SAP AG BC401 24
-
8/4/2019 bc401-P-1-50
49/50
2.25
SAP AG 2002
Compatibility and Conversion
No conversion takes place if type-compatible data objects are assigned
If non-type-compatible data objects are assigned, conversion does takeplace if a conversion rule is defined
When are two types compatible?
Two elementary types are compatible when theyhave exactly the same type and length (and, in thecase of packed numbers, the same number ofdecimal places ).
Two structure types are compatible when they have thesame structure and their components are .
Two table types are compatible if their row types are
compatible and their key definitions and table typesare the same.
typetype lengthlength
decimal placesdecimal places
same structuresame structure components are compatiblecomponents are compatible
row typesrow types
key definitionskey definitions table typestable typesare the same.are the same.
MOVE a TO b.b = a.
compatiblecompatiblecompatiblecompatible
If two data types are not compatible but there is a conversion rule, the system converts the source object
into the type of the target object when you assign values, perform calculations, or compare values.
For a full list of all conversion rules, refer to the ABAP syntax documentation for theMOVEstatement.
If there is no conversion rule defined for a particular assignment, the way in which the system reacts
depends on the program context.
If the types of the objects involved are defined statically, a syntax error occurs.
Example:DATA: date TYPE d VALUE '19991231', time TYPE t.
FIELD-SYMBOLS: TYPE d, TYPE t.
ASSIGN: date TO , time TO .
= . In the case ofdynamic typing a runtime error occurs, because the field symbols are not assigned types
until the assignment of the data objects at runtime.
Example:...
FIELD-SYMBOLS: TYPE ANY, TYPE ANY.
...
(Rest as above)
(C) SAP AG BC401 25
-
8/4/2019 bc401-P-1-50
50/50
2.26
SAP AG 2002
Conversion Under Unicode (1)
Conversion between flat structures
struc2
MOVE struc1 TO struc2.
Decisive criterion:Fragment view
IX(3)C(3)C(4) C(3) C(4)
struc1
C(6) N(4) X(3) N(4) I P(8)
struc1
struc2
P(8)IC(4)X(3)C(10)
Conversion between flat structures is possible if the fragments ofthe source and target structures match both in type and length inthe length of the shorter structure.
Under what conditionis this assignmentpossible?
Assignment is possible
For some data types there are, for technical reasons, specific alignment requirements that depend on the
individual platforms. (In the memory, fields of this type must begin and end on specific addresses - for
example, a memory address divisible by four.)
Within a structure, the runtime system, if necessary, inserts bytes before or after these components with
alignment requirements to achieve the alignment needed. These bytes are known as Alignment.
To check whether the conversion is even permitted, the system first creates the Unicode fragment view
of the structures by grouping adjacent components and alignment gaps (one group each for character-
type types [c, n, d, t], byte-type types, and types i, f, andp).
Adjacent character-type components of a structure are therefore only grouped together if there are no
alignment gaps between these components. Adjacent byte-type components are grouped in the same
way. If the fragments of the source and initial structures match the type and length in the length of the shorter
structure, the conversion is allowed.
If the target structure is longer than the source structure, the character-type components of the
remainder are filled with space characters. All other components in the remainder are filled with the