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

Difference between Type and Like

Former Member
0 Likes
895

Can anyone tell me the basic difference between type and like in variable declaration?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
856

Hi,

ABAP distinguishes between types and objects. Types are descriptions that do not occupy memory. Objects are instances of types, and do occupy their own memory space. A type describes the technical attributes of all of the objects with that type.

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program.

DATA <f> TYPE <type>.

The data object <f> has a data type corresponding to the type <type>.

DATA <f> LIKE <obj>.

The data object <f> inherits all of the technical attributes of the data object <obj>.

Take an example :

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab like ty_tab.

See here we declared the structure of ty_tab, which do not occupy memory. So if we run this, we will get compile time error like this : Field TY_TAB is unknown. It is neither in one of the specified tables nor defined by a DATA statement.

So in this case u need to correct the error with "TYPE" statement...like this.

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab type ty_tab.

Refer these

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

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

Thanks

6 REPLIES 6
Read only

Former Member
0 Likes
857

Hi,

ABAP distinguishes between types and objects. Types are descriptions that do not occupy memory. Objects are instances of types, and do occupy their own memory space. A type describes the technical attributes of all of the objects with that type.

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program.

DATA <f> TYPE <type>.

The data object <f> has a data type corresponding to the type <type>.

DATA <f> LIKE <obj>.

The data object <f> inherits all of the technical attributes of the data object <obj>.

Take an example :

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab like ty_tab.

See here we declared the structure of ty_tab, which do not occupy memory. So if we run this, we will get compile time error like this : Field TY_TAB is unknown. It is neither in one of the specified tables nor defined by a DATA statement.

So in this case u need to correct the error with "TYPE" statement...like this.

types : begin of ty_tab,

name(30),

pwd(10),

end of ty_tab.

data : itab type ty_tab.

Refer these

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

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

Thanks

Read only

Former Member
0 Likes
856

Hi,

Refer this link

Re: LIKE & TYPE

Posted: Jul 23, 2007 6:18 AM in response to: Mohit Goel Reply

HI

1)

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

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

TYPE means it is a predefined data type.

Eg:

DATA int TYPE i.

Here int is of integer data type.

DATA var LIKE int.

var IS a variable having same data type of int. which in turn is integer.

You can find these helpful when you reference database table variables... You need not know what is the datatype defined.

Also it adds to FLEXIBILITY.

Whenever you make changes to your database tables and fields,

that change is REFLECTED back to your program that is,

You need not change all your program code when you change your table fields...

2)Passing the table using the TABLES parameters in a function module is one solution. However, using the IMPORT or CHANGING parameters is a better solution. Just create in the Dictionary a table type like your internal table to be able to TYPE your parameters.

rgds,

bharat.

Read only

Former Member
0 Likes
856

HI,

TYPE is used to declare W.R.F to global field (EX. VBAK-VBELN) here u can not use like.

LIKE is used in declaring any object which is already created in program.

ex. DATA: <b>l_vbeln</b> <b>TYPE</b> vbak-vbelln.

DATA: g_vbeln <b>LIKE</b> <b>l_VBELN</b>.

NOTE: LIKE is obsolete.

Regds,

Read only

Former Member
0 Likes
856

hi gopi,

The basic difference is in,

TYPE, you can assign datatype directly to the data object while declaring. But in

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

Read only

Former Member
0 Likes
856

hi Gopi,

type will create structure,

like uses existing field as reference.

data declared can hold values

but structure can't hold values

RULE:

type: xxx type xxxx define type

type: xxx like xxxx create type

data : xxx type xxxx define field

data : xxx like xxxx create field

with Regards

muthu

Read only

Former Member
0 Likes
856

hi gopi,

through type you can create variable of your liking format

eg data: temp(6) type c.

temp is a character of length 6

data: tt like temp.

tt is another character with same format as of temp.

there is no much difference in the above example, but helps much when you create local structure of your own and need its more than one copy in a code