Application Development and Automation 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: 
Read only

TYPE vs LIKE

Former Member
0 Likes
8,415

Hi, would like to know what is the different btw the 2 declaration below: Which one is better? How the memory allocated for both?

MATNR TYPE MARA-MATNR.

MATNR LIKE MARA-MATNR.

Thank you.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
3,959

Hello Siow

Here is what the ABAP keyword documentation says to:

<b>DATA - TYPE, LIKE</b>

<i>In the specification of a data type type or a data object dobj, the data type of the variable dobj is already fully defined before the declaration. The syntax and meaning of the additions TYPE and LIKE has exactly the same meaning as the definition of data types with TYPES, except that for DATA after TYPE a standard table type with a generic table key can be specified. In this case, a bound table type with a standard key is created. </i>

However, with respect to ABAP-OO you should favour <b>DATA ... TYPE ...</b> because within classes and interfaces you can <b>only use TYPE</b> to refer to dictionary types.

Regards

Uwe

14 REPLIES 14
Read only

Former Member
3,959

Hi,

In your case there will be no difference. You can use any one.

Regards,

Atish

Read only

uwe_schieferstein
Active Contributor
3,960

Hello Siow

Here is what the ABAP keyword documentation says to:

<b>DATA - TYPE, LIKE</b>

<i>In the specification of a data type type or a data object dobj, the data type of the variable dobj is already fully defined before the declaration. The syntax and meaning of the additions TYPE and LIKE has exactly the same meaning as the definition of data types with TYPES, except that for DATA after TYPE a standard table type with a generic table key can be specified. In this case, a bound table type with a standard key is created. </i>

However, with respect to ABAP-OO you should favour <b>DATA ... TYPE ...</b> because within classes and interfaces you can <b>only use TYPE</b> to refer to dictionary types.

Regards

Uwe

Read only

Former Member
0 Likes
3,959

Hi,

LIKE means the datatype of the variable is similar to the referenced variable.

TYPE means it is a predefined data type.

Diff bn TYPE N LIKE.

If you are refering to a Data object use LIKE

And if you are refering to a Data type use TYPE

You can create a variable that inherits exactly the same technical attributes as an existing data type or data object as follows:

DATA <f> [TYPE <type>|LIKE <obj>]...

If you use the TYPE addition, <type> is any data type with fully-specified technical attributes. This can be a:

Non-generic predefined ABAP type (D, F, I, T, STRING, XSTRING)

Any existing local data type in the program.

Any ABAP Dictionary data type

If you use the LIKE addition, <obj> is a data object that has already been declared. This can also be a predefined data object. The variable <f> adopts the same technical attributes as the data object <obj>. You can also use LIKE to refer to a line of an internal table that has already been declared as a data object:

DATA <f> LIKE LINE OF <itab>.

To ensure compatibility with previous releases, <obj> can also be a database table, a view, a structure, or a component of a structure from the ABAP Dictionary.

The data types to which you refer can be elementary types, reference types, or complex types (structures or tables). For elementary field types, the variables are a single field in memory. When you declare a data type with fixed length (D, F, I, T) the system fixes the amount of memory that will be assigned. When you declare an object with a variable length (STRING, XSRTING), the system only assigns enough memory to administer the object. The length of the data object is managed dynamically at runtime. For structures, the variables are a sequence of variables, which may themselves also be included in further complex structures. The individual components take their name <ci> from the type <type> or object <obj>, and can be addressed using <f>-<c i> For tables, the memory contains administration entries that can be filled dynamically at runtime.

Go through the link.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

For TYPE

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

For LIKE

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb367a358411d1829f0000e829fbfe/content.htm

Regards,

Priyanka.

Read only

Former Member
3,959

Hi

<b>TYPE Vs LIKE</b>

In the specification of a data type type or a data object dobj, the data type of the variable dobj is already fully defined before the declaration. The syntax and meaning of the additions TYPE and LIKE has exactly the same meaning as the definition of data types with TYPES, except that for DATA after TYPE a standard table type with a generic table key can be specified. In this case, a bound table type with a standard key is created.

Prior to Release 6.10, no VALUE addition was possible if the data type deep, specified using TYPE or LIKE, was a string, a reference type, a table type, or a structured type with deep components. As of Release 6.10, the VALUE addition can also be used for deep data types; however, with the limitation that a start value val can only be specified for the ABAP type string, and otherwise only IS INITIAL.

These statements define two data objects, both of which have the same data type as the database table spfli.

DATA: spfli_wa1 TYPE spfli,

spfli_wa2 LIKE spfli_wa1.

<b>Reward if usefull</b>

Read only

varma_narayana
Active Contributor
3,959

Hi

When you refer to DDIC fields there is no difference. You can use either TYPE or LIKE.

But the basic difference between TYPE and LIKE is.

TYPES : NAME(20) TYPE C.

DATA : FIRSTNAME TYPE NAME . "Here we have to use TYPE bcoz we are

"referring to a Data type

DATA : LastNAME LIKE FIRSTNAME . "Here we have to use LIKE bcoz we are

"referring to a Variable.

REWARD IF HELPFUL.

Read only

Former Member
0 Likes
3,959

Hi,

pls chk this link:

hope it is helpful.

Regards,

Anjali

Read only

Former Member
0 Likes
3,959

Hi,

TYPE-you assign datatype directly to the data object while declaring.

LIKE-you assign the datatype of another object to the declaring data object. The datatype is referenced indirectly.

Type is a keyword used to refer to a data type whereas Like is a keyword used to copy the existing properties of already existing data object.

Regards,

Shanmugapriya

Read only

Former Member
0 Likes
3,959

Hi,

If you are refering to a pre-defined data type then use 'TYPE'. If you are refering to a data object then use 'LIKE'.

In terms of performance it is always advisable to use 'TYPE'. Even if you are not using a predefined data type, declare a custom data-type using 'TYPES' statement in your program and then use it in data declaration.

Eg: TYPES: var1 type char25.

DATA: var2 type var1.

Thanks,

Subathra

Read only

Former Member
0 Likes
3,959

Hi,

Both TYPe and LIKe serve the same purpose.

But LIKE is made obsolete from version ECC 5. So, we need to use TYPE for all the variable declarations.

In ABAP objects , LIKE is made completely obsolete , means it gives a syntax error.

But we can use LIKE for local data types.

Regards,

Lakshmi.

Read only

Former Member
0 Likes
3,959

Hi Siow Foong Chong ,

These r the differences between TYPE & LIKE:

1) With TYPE, you assign datatype directly to the data object while declaring.

With LIKE,you assign the datatype of another object to the declaring data object. The datatype is referenced indirectly.

2)Type is a keyword used to refer to a data type whereas Like is a keyword used to copy the existing properties of already existing data object.

3)type refers the existing data type

like refers the existing data object

4)The TYPE addition allows you to construct new data types in the TYPES, DATA, CONSTANTS, and STATICS statements. 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.

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

A)Construction of reference types

REF TO type|dobj

B)Construction of structured data types

BEGIN OF struc_type.

...

END OF struc_type.

C)Construction of table types

tabkind OF linetype [WITH key]

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

6) We can refer to a predefined ABAP type, an existing local data type in the program and a data type in the ABAP Dictionary using the TYPE addition where as We can refer to the data type of a local data object in the program using the LIKE addition

7) To ensure compatibility with earlier releases, we 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.

By peformance wise TYPE is good.

These r some websites which u may find useful.

http://help.sap.com/saphelp_nw04/helpdata/en/9b/239fa610de11d295390000e8353423/content.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm

if u find this useful , pls reward points.

Thanks & Regards,

gyanaraj

s_gyanaraj@yahoo.co.in

Read only

Former Member
0 Likes
3,959

for type no memory is allocated,for like memory is allocated.

one more difference is you will use type for new objects(ex:name type c) and like to use predifined objects to define new objects(ex:matr like matnr "matnr is already existed one)

Read only

Former Member
0 Likes
3,959

Hi,

There is no much difference in both. LIKE may be obsolete in some cases.

LIKE should be used only when you are refering to a field of standard table.

For example:MATNR LIKE MARA-MATNR.

Read only

Former Member
0 Likes
3,959

TYPE has dynamic allocation of memory and LIKE has static memmory allocation. that is the difference memory allocation wise. but you use TYPE basically to declare a type of predefiined ABAP type and to declare the type of usedefined type you use LIKE stmt.

Read only

Former Member
0 Likes
3,959

HI,

TYPES :

EX : I1 TYPE MARA-MATNR.

i1 IS DEFINED AS TYPE MATNR.

NOW IN UR PROGRAM WHEN EVER U WANT TO USE A FIELD WITH SIMILAR CHARACTERISTICS OF MARA-MATNR, U WILL BE USING LIKE THIS

TEMP LIKE I1,

TEMP1 LIKE I1,

LIKE THIS WE CAN CREATE OUR OWN DATA TYPES

THIS ""TYPES """ USAGE IS PARTICULARLY USED WHEN U GO FOR ""OOABAP""

LIKE :

I1 LIKE MARA-MATNR.

WHENEVER WE WANT TO USE OTHER VARIABLE LIKE MARA-MATNR ,

WE HAVE TO DECALRE THAT AGAIN

TEMP LOIKE MARA-MATNR,

TEMP1 LIKE MARA-MATNR

THIS IS USED IN NORMAL ABAP PROGRAMING