Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between TYPES and DATA...

Former Member

Hi frnds,

What is the difference between TYPES and DATA.... I have given example below....

TYPES: BEGIN OF I_EKKO,

EBELN LIKE EKKO-EBELN,

AEDAT LIKE EKKO-AEDAT,

BUKRS LIKE EKKO-BUKRS,

BSART LIKE EKKO-BSART,

LIFNR LIKE EKKO-LIFNR,

END OF I_EKKO.

DATA: BEGIN OF I_EKKO,

EBELN LIKE EKKO-EBELN,

AEDAT LIKE EKKO-AEDAT,

BUKRS LIKE EKKO-BUKRS,

BSART LIKE EKKO-BSART,

LIFNR LIKE EKKO-LIFNR,

END OF I_EKKO.

Thanx in Adv,

Reg,

<b>Suresh.V</b>

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Each ABAP program define its own data types using the statement.

TYPES dtype TYPE type ...

and declare its own variables or instance attributes of classes using the statement

DATA var {TYPE type} ...

Within the program or a class, you can also define local data types and variables within procedures. Local variables in procedures obscure identically-named variables in the main program or class.

When creating data types and data objects, there are a number of naming convention that also apply for other local program definitions, such as procedures. These are described in detail in the keyword documentation.

The Additions TYPE and LIKE


The additions TYPE type and LIKE dobj are used in various ABAP statements. The additions can have various meanings, depending on the syntax and context.

· Definition of local types in a program

· Declaration of data objects

· Dynamic creation of data objects

· Specification of the type of formal parameters in subroutines

· Specification of the type of formal parameters in methods

· Specification of the type of field symbols

Constructing New Data Types

The TYPE addition allows you to construct new data types in the TYPES, DATA; CONSTANTS; and STATICSstatements. In the TYPES statement, these are local data types in the program. In the other statements, they are attributes of new data objects, meaning that the newly defined data types are not free-standing. Rather, they are linked to database objects.This means that you can refer to them using the LIKEaddition, but not using TYPE.

To construct new data types, the addition TYPE can be used with the following type constructors:

· Construction of reference types

REF TO type|dobj

· Construction of structured data types

BEGIN OF struc_type.

...

END OF struc_type.

· Construction of table types

tabkind OF linetype

These data types only exist during the runtime of the ABAP program

Referring to Known Data Types or Data Objects
Using the additions TYPE or LIKE in the TYPESstatement, local data types in a program can be referred to known data types or data objects. This is mainly the case with user-defined elementary data types. If you declare variables using the additions TYPE type or LIKE dobj with statement DATA, the data type of var is already fully defined before the declaration is made.

The known types or data that are referred to must be visible at the point where the data type or variable is declared.

A known data type can be any of the following:

· A predefined ABAP type to which you refer using the TYPE addition

· An existing local data type in the program to which you refer using the TYPE addition

· The data type of a local data object in the program to which you refer using the LIKE addition

· A data type in the ABAP Dictionary to which you refer using the TYPE addition. To ensure compatibility with earlier releases, it is still possible to use the LIKE addition to refer to database tables and flat structures in the ABAP Dictionary. However, you should use the TYPE addition in new programs.

The LIKE addition takes its technical attributes from a visible data object. As a rule, you can use LIKE to refer to any object that has been declared using DATA or a similar statement, and is visible in the current context. The data object only has to have been declared. It is irrelevant whether the data object already exists in memory when you make the LIKE reference.

· In principle, the local data objects in the same program are visible. As with local data types, there is a difference between local data objects in procedures and global data objects. Data objects defined in a procedure obscure other objects with the same name that are declared in the global declarations of the program.

· You can also refer to the data objects of other visible ABAP programs. These might be, for example, the visible attributes of global classes in class pools. If a global class cl_lobal has a public instance attribute or static attribute attr, you can refer to it as follows in any ABAP program:

DATA dref TYPE REF TO cl_global.

DATA: f1 LIKE cl_global=>attr,
f2 LIKE dref->attr.

You can access the technical properties of an instance attribute using the class name and a reference variable without first having to create an object. The properties of the attributes of a class are not instance-specific and belong to the static properties of the class.

Ex.
TYPES: BEGIN OF struct,
number_1 TYPE i,
number_2 TYPE p DECIMALS 2,
END OF struct.

DATA: wa_struct TYPE struct,
number LIKE wa_struct-number_2,
date LIKE sy-datum,
time TYPE t,
text TYPE string,
company TYPE s_carr_id.

This example declares variables with reference to the internal type STRUCT in the program, a component of an existing data object wa_struct, the predefined data object SY-DATUM, the predefined ABAP type t and STRING, and the data element S_CARR_ID from the ABAP Dictionary.

Referring to Generic Data Types

If you refer to one of the generic predefined ABAP types of fixed length (c, n, p, x) in the TYPES or DATA statement, you must specify the undefined technical attributes.

TYPES|DATA var[(length)] TYPE type ...

TYPES|DATA var TYPE type ...

Ex.

DATA: text1,

text2 LENGTH 2,

text3 TYPE c LENGTH 3,

pack TYPE p DECIMALS 2 VALUE '1.225'.

This example creates three character variables with field lengths of one, two, and three bytes respectively, and a packed number variable with field length 8 bytes and two decimal places. If the attribute Fixed point arithmetic is set, the value of pack is 1.23.

This example shows how to declare elementary data objects with reference to predefined ABAP types.

PROGRAM demo_elementary_data_objects.

DATA text1 TYPE c LENGTH 20.

DATA text2 TYPE string.

DATA number TYPE i.

text1 = 'The number'.

number = 100.

text2 = 'is an integer.'.

WRITE: text1, number, text2.

This program produces the following output on the screen:

The number 100 is an integer.

In this example, the data objects text1, text2 and number are declared with the DATA statement. The technical attributes are determined by referring to the predefined ABAP types c, string, and I. Values from unnamed literals are assigned to the data objects. The contents of the named data objects are displayed on the list.

Specifying a Start Value

When you declare an elementary fixed-length variable, the DATAstatement automatically fills it with the type-specific initial value as listed in the table in the Predefined ABAP Types section.

However, you can also specify a starting value of a fixed-length elementary variable (also within a structure declaration) using the VALUE addition in the DATAstatement:

DATA var ... VALUE val|{IS INITIAL}.

Specifying start values:

DATA: counter TYPE p VALUE 1,

date TYPE d VALUE '19980601',

flag TYPE n VALUE IS INITIAL.

After this data declaration, the character string flag contains its type specific

Initial value ‘0’.

regards,

Bhaskar

10 REPLIES 10

Former Member

Hi Suresh ,

When you define a type no memory is allocated where as for a structure memory is allocated as soon as it is defined.

Convert the code you have provided and see what happens


TYPES: BEGIN OF tI_EKKO,
EBELN LIKE EKKO-EBELN,
AEDAT LIKE EKKO-AEDAT,
BUKRS LIKE EKKO-BUKRS,
BSART LIKE EKKO-BSART,
LIFNR LIKE EKKO-LIFNR,
END OF tI_EKKO.

DATA: BEGIN OF I_EKKO,
EBELN LIKE EKKO-EBELN,
AEDAT LIKE EKKO-AEDAT,
BUKRS LIKE EKKO-BUKRS,
BSART LIKE EKKO-BSART,
LIFNR LIKE EKKO-LIFNR,
END OF I_EKKO.

break-point.

now execute the program and at the breakpoint try to check value of I_EKKO and tI_EKKO. ONLY I_EKKO will be allocated meomory tI_EKKO will not be.

Hope this helps , reward points for helpful replies.

Regards

Arun

suresh_datti
Active Contributor

With TYPES you are defining the data types that can be used by other variables/itabs in the program

With DATA you are actually declaring the Variables with or without referencing the TYPES.

~Suresh

Former Member
0 Kudos

Hi,

The first declaration creates the structure.

The second declaration creates a workarea.

ferry_lianto
Active Contributor

Hi,

1. TYPES statement defines the structure without allocation of memory, DATA statements allocate the memory at runtime.

2. Create a structure by using types statement and refer it by using the DATA statement.

3. In order to avoid the internal table declaration without header line, we are declaring a structure by types statemnet and declaring an internal table with reference to the structure.

Regards,

Ferry Lianto

former_member184112
Active Contributor
0 Kudos

Hi,

TYPE

Defines the type through reference to a data type.

LIKE

Defines the type through reference to a data object.

Thanks and Regards,

Prabhakar Dharmala

Former Member
0 Kudos

The declaration with TYPEs as good as ABAP dictionary object for your local program.

The one with DATA is like a varaibles. You can use it only by using LIKE keyword but not TYPE

Former Member
0 Kudos

Hi,

The main difference between TYPE and LIKE parameter when defining or declaring the object is that TYPE is used to refer existing DATA TYPE (elementary or structured or user defined) while LIKE is used to declare data objects with reference to existing DATA OBJECTS.

Consider the following example.

DATA: fname(20),

mname(20),

lname(20),

add1(20),

add2(20),

add3(20).

If you have DATA statement like above, and if you need to change the length of all the fields say from 20 to 25, then you need to change all the fields i.e., going through each and every statement.

But consider the following case where TYPES has been used.

TYPES:str(20)

DATA:fname type str,

Mname type str,

Lname type str,

Add1 type str,

Add2 type str,

Add3 type str.

In this case if you need to change the length of all fields from 20 to 25. Then just change the length of STR and change will be reflected for all the fields.

Regards

Jitendra

Former Member
0 Kudos

Hi,

Each ABAP program define its own data types using the statement.

TYPES dtype TYPE type ...

and declare its own variables or instance attributes of classes using the statement

DATA var {TYPE type} ...

Within the program or a class, you can also define local data types and variables within procedures. Local variables in procedures obscure identically-named variables in the main program or class.

When creating data types and data objects, there are a number of naming convention that also apply for other local program definitions, such as procedures. These are described in detail in the keyword documentation.

The Additions TYPE and LIKE


The additions TYPE type and LIKE dobj are used in various ABAP statements. The additions can have various meanings, depending on the syntax and context.

· Definition of local types in a program

· Declaration of data objects

· Dynamic creation of data objects

· Specification of the type of formal parameters in subroutines

· Specification of the type of formal parameters in methods

· Specification of the type of field symbols

Constructing New Data Types

The TYPE addition allows you to construct new data types in the TYPES, DATA; CONSTANTS; and STATICSstatements. In the TYPES statement, these are local data types in the program. In the other statements, they are attributes of new data objects, meaning that the newly defined data types are not free-standing. Rather, they are linked to database objects.This means that you can refer to them using the LIKEaddition, but not using TYPE.

To construct new data types, the addition TYPE can be used with the following type constructors:

· Construction of reference types

REF TO type|dobj

· Construction of structured data types

BEGIN OF struc_type.

...

END OF struc_type.

· Construction of table types

tabkind OF linetype

These data types only exist during the runtime of the ABAP program

Referring to Known Data Types or Data Objects
Using the additions TYPE or LIKE in the TYPESstatement, local data types in a program can be referred to known data types or data objects. This is mainly the case with user-defined elementary data types. If you declare variables using the additions TYPE type or LIKE dobj with statement DATA, the data type of var is already fully defined before the declaration is made.

The known types or data that are referred to must be visible at the point where the data type or variable is declared.

A known data type can be any of the following:

· A predefined ABAP type to which you refer using the TYPE addition

· An existing local data type in the program to which you refer using the TYPE addition

· The data type of a local data object in the program to which you refer using the LIKE addition

· A data type in the ABAP Dictionary to which you refer using the TYPE addition. To ensure compatibility with earlier releases, it is still possible to use the LIKE addition to refer to database tables and flat structures in the ABAP Dictionary. However, you should use the TYPE addition in new programs.

The LIKE addition takes its technical attributes from a visible data object. As a rule, you can use LIKE to refer to any object that has been declared using DATA or a similar statement, and is visible in the current context. The data object only has to have been declared. It is irrelevant whether the data object already exists in memory when you make the LIKE reference.

· In principle, the local data objects in the same program are visible. As with local data types, there is a difference between local data objects in procedures and global data objects. Data objects defined in a procedure obscure other objects with the same name that are declared in the global declarations of the program.

· You can also refer to the data objects of other visible ABAP programs. These might be, for example, the visible attributes of global classes in class pools. If a global class cl_lobal has a public instance attribute or static attribute attr, you can refer to it as follows in any ABAP program:

DATA dref TYPE REF TO cl_global.

DATA: f1 LIKE cl_global=>attr,
f2 LIKE dref->attr.

You can access the technical properties of an instance attribute using the class name and a reference variable without first having to create an object. The properties of the attributes of a class are not instance-specific and belong to the static properties of the class.

Ex.
TYPES: BEGIN OF struct,
number_1 TYPE i,
number_2 TYPE p DECIMALS 2,
END OF struct.

DATA: wa_struct TYPE struct,
number LIKE wa_struct-number_2,
date LIKE sy-datum,
time TYPE t,
text TYPE string,
company TYPE s_carr_id.

This example declares variables with reference to the internal type STRUCT in the program, a component of an existing data object wa_struct, the predefined data object SY-DATUM, the predefined ABAP type t and STRING, and the data element S_CARR_ID from the ABAP Dictionary.

Referring to Generic Data Types

If you refer to one of the generic predefined ABAP types of fixed length (c, n, p, x) in the TYPES or DATA statement, you must specify the undefined technical attributes.

TYPES|DATA var[(length)] TYPE type ...

TYPES|DATA var TYPE type ...

Ex.

DATA: text1,

text2 LENGTH 2,

text3 TYPE c LENGTH 3,

pack TYPE p DECIMALS 2 VALUE '1.225'.

This example creates three character variables with field lengths of one, two, and three bytes respectively, and a packed number variable with field length 8 bytes and two decimal places. If the attribute Fixed point arithmetic is set, the value of pack is 1.23.

This example shows how to declare elementary data objects with reference to predefined ABAP types.

PROGRAM demo_elementary_data_objects.

DATA text1 TYPE c LENGTH 20.

DATA text2 TYPE string.

DATA number TYPE i.

text1 = 'The number'.

number = 100.

text2 = 'is an integer.'.

WRITE: text1, number, text2.

This program produces the following output on the screen:

The number 100 is an integer.

In this example, the data objects text1, text2 and number are declared with the DATA statement. The technical attributes are determined by referring to the predefined ABAP types c, string, and I. Values from unnamed literals are assigned to the data objects. The contents of the named data objects are displayed on the list.

Specifying a Start Value

When you declare an elementary fixed-length variable, the DATAstatement automatically fills it with the type-specific initial value as listed in the table in the Predefined ABAP Types section.

However, you can also specify a starting value of a fixed-length elementary variable (also within a structure declaration) using the VALUE addition in the DATAstatement:

DATA var ... VALUE val|{IS INITIAL}.

Specifying start values:

DATA: counter TYPE p VALUE 1,

date TYPE d VALUE '19980601',

flag TYPE n VALUE IS INITIAL.

After this data declaration, the character string flag contains its type specific

Initial value ‘0’.

regards,

Bhaskar

Former Member
0 Kudos

Hi

To keep it simple if you want to declare a table then

for first case you'll require to mention it as type table of and in the second case you'll define it as like table of.

Cheers

Sudip

Former Member
0 Kudos

Hi Guys....

Thanks for all of you....

Reg,

Suresh.V