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 b/w LIKE and TYPE

vinod_vemuru2
Active Contributor
0 Kudos

Hi,

Can anybody explain the exact difference between LIKE and TYPE with some example. As a programming standard we shouldnot use LIKE except in ALV reports. Is there any strong reason behind this other than LIKE is not allowed in OOPS concepts.

Thanks,

Vinod.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

10 REPLIES 10

Former Member
0 Kudos

naimesh_patel
Active Contributor
0 Kudos

LIKE refers the fields where as the TYPE wears all characteristic of that field.

Regards,

Naimesh Patel

Former Member
0 Kudos

HI

TYPE

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

LIKE

You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

type we can use to refer system defined or user defined data types and data dictionary objects..

like we will use in case of refering data dictionary objects only.

Go through the link.

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

TYPE - Type is used to tell the system what is the type of data object(variable) you want to create .

LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.

type is generally used for declaring variables, parameters for existing data types in abap

for ex: to declare a inter value and character variable of length 10 is as,

data: i1 type i,

c1(10) type c.

like generally refers to existing data objects in abap.

for ex:

data: matnr like mara-matnr,

vbeln like vbap-vbeln.

creating variables matnr, vbeln from existing fields of tables mara, vbap.

when user creates a user defiend structure for work areas, internal tables we generally use type keyword as

types: begin of itab,

.........

.........

........

end of itab.

data: itab1 type itab occurs 0 [with header line]

Check this thread.

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=2270752

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=512214

Regards

former_member200338
Active Contributor
0 Kudos

Hi,

LIKE - > say you know the internal table name. but you dont know how it is delcred. you want to copy the contents of this table into a dummy table and do some operation. then you go for LIKE statment,

data lt_xlikp like standard table of xlikp.

TYPE: you know the data type. it has been created using transcation SE11. it might be a strucutre or a dataelment. you want to use it. then type is used,

data ls_vbap type vbap.

reward points if useful,

Regards,

Niyaz

Former Member
0 Kudos

Hi,

TYPE -- used on elementory data types

LIKE -- can be used on any data object know at a point of time in ABAP

we can use LIKE and TYPE with out any difference in ABAP, where as in OOPS u can use LIKE

Former Member
0 Kudos

HI

<b>TYPE</b>

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

<b>LIKE</b>

You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

type we can use to refer system defined or user defined data types and data dictionary objects..

like we will use in case of refering data dictionary objects only.

Go through the link.

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

<b>TYPE</b> - Type is used to tell the system what is the type of data object(variable) you want to create .

LIKE: If there is already a data object declared and you want to declare a similar data object you can just refer to the previous data object using like.

type is generally used for declaring variables, parameters for existing data types in abap

for ex: to declare a inter value and character variable of length 10 is as,

data: i1 type i,

c1(10) type c.

like generally refers to existing data objects in abap.

for ex:

data: matnr like mara-matnr,

vbeln like vbap-vbeln.

creating variables matnr, vbeln from existing fields of tables mara, vbap.

when user creates a user defiend structure for work areas, internal tables we generally use type keyword as

types: begin of itab,

.........

.........

........

end of itab.

data: itab1 type itab occurs 0 [with header line]

Check this thread.

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=2270752

https://forums.sdn.sap.com/click.jspa?searchID=711746&messageID=512214

<b>Reward if usefull</b>

Former Member
0 Kudos

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.

For all practical purposes there are the same. The only additional advantage with types is that you can define your own types(including complex ones) in the data dictionary and reuse them accross various programs.

But within a program if two variables are defined one using LIKE and another using TYPE, both referring to the same field, then there is no difference.

If I include a type pool within a program, then I can define my variables only using TYPE to refer to any type defined in that pool. I cannot use LIKE in this scenario. Also, if I want to use native types like C, N, etc, I cannot use LIKE there either. I can use LIKE ABC only if ABC is in the database or if ABC is defined previously in the same program.

I can use TYPE ABC, if ABC is defined in database as a TYPE and included in the program with the statement TYPE-POOLS. I can use it, if it is the native types. I can use it, if it is already defined in the dictionary as a structure/table or structure/table field, or even if it is defined as a data element or a domain. So I can declare a variable V_BUKRS TYPE BUKRS, but I cannot define a variable V_BUKRS LIKE BUKRS.

But if I intend to use V_BUKRS to store company code, I will prefer to declare it as V_BUKRS LIKE T001-BUKRS, only because if tomorrow for some reason, the definition of T001-BUKRS changes to a data element for example, BUKRS_N(say DEC 4) instead of the data element BUKRS(CHAR 4) that it refers to now, I don't have to change my programs because I am referring to the table field and inhereting its properties. Whereas, had I declared my V_BUKRS TYPE BUKRS and the table now changed to BUKRS_N, I will be forced to change my program as there will be a type incompatability.

Former Member
0 Kudos

Refer the links -

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

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

LIKE is used inorder to refer to a present object and to have the semantic definition of that object that is being refered to.

TYPE is used in declaring the object using an elementary data type or a data element which has the same semantic definition that is required.

It is good practice to use TYPE as much as possible when there is a possibility to avoid LIKE.

TYPE

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

LIKE

You use the LIKE addition, similarly to the TYPE addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols.

it can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

The LIKE Addition:

You use the LIKE addition, similarly to the TYPE addition, in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression

LINE OF <table-object>

In this case, the LIKE addition describes the line type of a table object that is visible at that point in the program.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

The TYPE Addition:

You use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

Referring to Known Data Types You can use the addition

TYPE <type> to refer to any data type <type> that is already known at this point in the program. It can be used in any of the statements listed below. The expression <obj> is either the name of the data object or the expression

LINE OF <table-type> In this case, the TYPE addition describes the line type of a table type <table-type> that is visible at that point in the program.

use this links...

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

for like....

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

mahaboob_pathan
Contributor
0 Kudos

Hi,

Type : Is used to invoke the pre-defined data types.

for example :

Data : nam(10) type c.

expalnation of the above synatx..

nam(10) is data object.

type is a keyword to define the data object.

C is a pre-defined data type.

and

Like : Is used to invoke the pre-defined data objects.

for example

Data : nam(10) type c.

data : firstnam like nam,

midlenam like nam,

lastname like nam.

above syntax explanation,

nam is data object defined by predifnd data type.

firstnam, midlenam, lastnam are data objects defined with refferance to defined Data 0bjects with the keyword called LIKE.

vinod_vemuru2
Active Contributor
0 Kudos

Thanks all