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

Help with TYPE and LIKE statements

Former Member
0 Likes
870

HI guys,

I know this is really novice stuff, but I am a little confused.

Can anyone please explain the exact difference between TYPE and like with the help of a program, to understand it.

What situation would demand the use of each of the LIKE statement, since I can do all these things using the TYPE ?

1 ACCEPTED SOLUTION

Hi Akhil,

I summarized the info in SDN posts and SAP Help, to make it easier for you to understand. I also included some code snippets. Hope these prove to be helpful to you.

The following is from SAP Help:

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

***************************************************************

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.

Example

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.

***********************

The following info is from various posts:

--> Type: It is used when userdefined object link with SAP system data type.

Local types mask global types that have the same names. When typing the interface parameters or field symbols, a reference is also possible to generic types ANY, ANY TABLE,INDEX TABLE, TABLE or STANDARD TABLE, SORTED TABLE and HASHED TABLE.

--> Like: It is when data object link with the other data object.

--> 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.

you can refer to all visible data objects at the ABAP program's positon in question. Only the declaration of the data object must be known. In this case it is totally irrelevant whether the data object already exists physically in

memory during the LIKE reference. Local data objects mask global data objects that have the same name.

--> 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.

Types: var1(20) type c.

data: var2 type var1. ( type is used bcoz var1 is defined with TYPES and it

does not occupy any memory spce.

data: var3 like var2. ( like is used here bcoz var2 is defined with DATA

so it does occupy space in memory ).

data: material like mara-matnr. ( like is used here bcoz mara-matnr is stored in memory)

--> Type refers the existing data type

--> Like refers the existing data object

-

-


Please Reward Points if any of the above points are helpful to you.

Regards,

Kalyan Chakravarthy

8 REPLIES 8
Read only

Former Member
0 Likes
830

Hi Akhil,

type refers the existing data type

like refers the existing data object

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.

Please check this thread.,

Reward points if useful,

Cheers,

Swamy Kunche

Read only

Former Member
0 Likes
830

Hello,

See this: [http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/content.htm] and .

Regards.

Read only

Former Member
0 Likes
830

Hi Akhil,

I summarized the info in SDN posts and SAP Help, to make it easier for you to understand. I also included some code snippets. Hope these prove to be helpful to you.

The following is from SAP Help:

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

***************************************************************

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.

Example

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.

***********************

The following info is from various posts:

--> Type: It is used when userdefined object link with SAP system data type.

Local types mask global types that have the same names. When typing the interface parameters or field symbols, a reference is also possible to generic types ANY, ANY TABLE,INDEX TABLE, TABLE or STANDARD TABLE, SORTED TABLE and HASHED TABLE.

--> Like: It is when data object link with the other data object.

--> 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.

you can refer to all visible data objects at the ABAP program's positon in question. Only the declaration of the data object must be known. In this case it is totally irrelevant whether the data object already exists physically in

memory during the LIKE reference. Local data objects mask global data objects that have the same name.

--> 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.

Types: var1(20) type c.

data: var2 type var1. ( type is used bcoz var1 is defined with TYPES and it

does not occupy any memory spce.

data: var3 like var2. ( like is used here bcoz var2 is defined with DATA

so it does occupy space in memory ).

data: material like mara-matnr. ( like is used here bcoz mara-matnr is stored in memory)

--> Type refers the existing data type

--> Like refers the existing data object

-

-


Please Reward Points if any of the above points are helpful to you.

Regards,

Kalyan Chakravarthy

Read only

Former Member
0 Likes
830

Hi,

This is an explanation of differences between TYPE and LIKE additions

Type: It is used when userdefined object link with SAP

system data type.

Like: It is when data object link with the other data

object.

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.

Additional key words

Data types, data objects, ABAP type concept, Dictionary reference

Cause and prerequisites

The ABAP type concept distinguishes between data types and data objects.

o Data types are either defined locally in an ABAP program with TYPES

or globally in the ABAP Dictionary.

o Data objects reside in the internal mode of an ABAP program and are

declared prototypical with the statement DATA.

When you declare local program data types with TYPES or data objects with

DATA, the data type must be defined. To do this you can refer to already

existing types in addition to using type constructors, for example, TYPE

TABLE OF or TYPE REF TO. It is possible to refer to data types using the

TYPE addition and the data object using the LIKE addition. Furthermore,

type references are possible using TYPE and LIKE also during typing

interface parameters or field symbols.

o TYPE addition

With TYPE addition, you can refer either to local data types of the

same ABAP program or on global data types of the Dictionaries.

Local types mask global types that have the same names. When typing

the interface parameters or field symbols, a reference is also

possible to generic types ANY, ANY TABLE,INDEX TABLE, TABLE or

STANDARD TABLE, SORTED TABLE and HASHED TABLE.

o The LIKE addition

With the LIKE addition, you can refer to all visible data objects

at the ABAP program's positon in question. Only the declaration of

the data object must be known. In this case it is totally

irrelevant whether the data object already exists physically in

memory during the LIKE reference. Local data objects mask global

data objects that have the same name.

The semantic separation between data types and data objects is reflected by

the syntactic separation in ABAP between TYPE and LIKE. This separation

allows for separate namespaces for data types and data objects. Only for

reasons of compatibility with preceding releases, can you refer to flat

structures (see note 176336) in the ABAP Dictionary with the LIKE addition,

so to database tables and independent flat structures. The LIKE addition

first finds a data object on the local program and then in the ABAP

Dictionary it finds a database table or flat structure that has the same

name. The type reference on the ABAP Dictionary is no longer possible with

LIKE in the class of ABAP objects. In all ABAP programs, the LIKE reference

25.06.2007 Page 2 of 2

Note 176337 - On the difference between TYPE and LIKE

is forbidden on data elements, internal tables and deep structures of the

ABAP Dictionary.

The TYPE reference to global data types of the ABAP Dictionary is possible

as of Release 4.5. At the same time all types of the ABAP type concept can

be defined in the ABAP Dictionary as of Release 4.5. Previously there was

only an actual equivalent for flat structures between the ABAP data types

and the dictionary data types. The structure of database tables and

independent structures corresponded to the ABAP data type structure in the

dictionary Only with the LIKE addition could database tables and structures

be referred to from ABAP programs. In the meantime the LIKE addition should

be used in all ABAP programs only for referring to data objects while the

TYPE addition is used of the reference to the data type.

Caution should be taken if a flat structure is changed in the dictionary by

adding or changing a component into a deep structure. Then a reference with

the LIKE reference is syntactically no longer possible and the ABAP

programs must be changed appropriately. As outlined in Note 176336, such a

structure change means more than replacing the LIKE with the TYPE addition.

Moreover the use of the structure must also be checked at other positions.

please check the links :

http://219.153.41.162/down4/20070625/25135751683.pdf

http://techpreparation.com/computer-interview-questions/sap-abap-interview-questions-

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

Reward if found useful,,,,,,,,

Regards

Sandeep Reddy

Read only

Former Member
0 Likes
830

Hi Akhil,

I understand that u are new to SAP and trying to explain it in bit simpler manner.

We use TYPE when we are refering to any object TYPE. (May be standard ones like C,P,I,N,F or user defiended TYPES using the TYPES clause)

We use LIKE, when we declare a variable or data object refering to an existing data object.

Declaring user defined TYPE

TYPES:BEGIN OF ty_mara,

matnr TYPE mara-matnr,

END OF ty_mara.

Declaring Data Object using TYPE

DATA: wa_mara TYPE ty_mara. "via User defined TYPE

w_matnr TYPE C. "via standard TYPE

Declaring Data Object using TYPE

DATA: wa_mara2 LIKE wa_mara. "via existing data object

Reward if helpful.

Read only

abdul_hakim
Active Contributor
0 Likes
830

TYPE:

For referring to the data types.

For eg.

DATA var1 TYPE i.

LIKE:

For referring to the exisiting data objects.

For eg.

DATA var2 LIKE var1.

Regards,

Hakim>

Read only

Former Member
0 Likes
830

Hi everyone,

Thanks for your prompt responses.

I guess i needed to be more complete in my question.

I understand the basic difference between these that declaration using

DATA corresponds to LIKE

and TYPES corresponds to TYPE.

My confusion is that, I have seen real time people use LIKE statements even if its possible to use TYPES, for example, while declaring structures at the start of a report..

I need to know if there are any specific situations (except PARAMETERS) whre only LIKE would to be used and not TYPE.

Is it just to improve the program readability?

Regards

Akhil

Edited by: akhil patel on May 29, 2008 7:42 PM