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: 

What is the difference between Type and Like?

Former Member
0 Kudos
203

Hi

What is the difference between Type and Like?

Thanks

8 REPLIES 8

Former Member
0 Kudos
144

to refer to already existing datatypes use 'TYPE'.

and to refer the user defined datatypes use 'LIKE'.

Check this documentation.

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

Former Member
0 Kudos
144

Hi subhash,

1.

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.

2. try this code (just copy paste)

report abc.

types : char50(50) type c.

*----


type.

data : d1 type c, "--- native

d2 type n, "--- native

d25 type char50 , "----


User defined data type

d3 type bukrs, "---- data element / domain

d4 type persno, "---- data element / domain

d5 type t001, "---- table

d99 type c

.

data :

*l1 like c "----


Not Allowed

*l2 like n "----


Not Allowed

*l25 like char50 , "----


User defined data type

*l3 like bukrs "----


Not Allowed

*l4 like persno, "----


Not Allowed

l5 like t001 , "---- table

l99 like pa0001

.

I hope it helps.

regards,

amit M.

Former Member
0 Kudos
144

... TYPE REF TO type

Defining a typed data reference variable. Types you can use include elementary types, types defined using TYPES, or types created in the ABAP Dictionary. You can dereference completely typed data reference variables using the dereferencing operator ->* at any operand position.

Example

types:

LNTYP type I.

data:

CNT type ref to I,

LINE type ref to LNTYP,

FLIGHT type ref to SFLIGHT.

create data CNT.

create data LINE.

create data FLIGHT.

CNT->* = 20.

LINE->* = 110.

FLIGHT->FLDATE = SY-DATUM.

... TYPE type

Effect

The field f is created with type type. For the type, you can specify either one of the predefined types listed below, a type defined using the TYPES statement, or a type created in the ABAP Dictionary.

The standard length ( SL ) of a field depends on its type.

Type Explanation SL Initial value

C Text (Character) 1 space

N Numeric text 1 '00...0'

D Date (YYYYMMDD) 8 '00000000'

T Time (HHMMSS) 6 '000000'

X Hexadecimal (HeX code) 1 X'00'

I Integer 4 0

P Packed number 8 0

F Floating point number 8 0

STRING Character sequence (string) variable-length empty string

XSTRING Byte sequence (X string)

variable-length empty hexadecimal string

Example

DATA NUMBER TYPE I.

DATA WA_SPFLI TYPE SPFLI.

The field NUMBER is created with type I. You can now use it in the program. In particular, you can assign numeric values to the field and use it in calculations ( ABAP number types).

The field WA_SPFLI is created using the type of the database table SPFLI from the ABAP Dictionary. This field is structured and can be used especially for working with data from database table SPFLI.

... LIKE f1

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas.

See Cannot Use LIKE References to Dictionary Types.

Effect

Field f is created with the same field attribtues as the data object f1, which has already been declared. Any data object (field, parameter, structure...) is allowed as long as its type has been fully specified.

f1 can be any ABAP Dictionary reference.

Example

DATA TABLE_INDEX LIKE SY-TABIX.

The field TABLE_INDEX now has the same attributes as SY-TABIX (index field for internal tables).

Note

You should use this addition whenever you can. If the type of a field to which you are referring changes, the ABAP runtime system updates all references automatically. It also stops the system from carrying out unnecessry (and maybe undesirable) type conversions.

Former Member
0 Kudos
144

to refer to already existing datatypes use 'LIKE'.

and to refer the user defined datatypes use 'TYPE'.

ex: DATA: p_pernr LIKE vbak-pernr.

TYPES: BEGIN of itab,

-


END of itab.

DATA: it_tab TYPE itab.

Former Member
0 Kudos
144

Hi,

Find the clear difference in my earlier thread, click the link below:

Reward if convinced.

Regards

Former Member
0 Kudos
144

Hi Subhash,

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

ie something like char , float, int that we have in C.

with ABAP you can have additional user defined types which you can create using the TYPES statement and using the already existent types .(something like structures).

This can be done in either SE38 ABAP editor or in SE11(ABAP dictionary).

DATA: this sattement is used to declare you varible as in c to tell the system that you will be using a data object which is of the type you have mentioned.

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.

Thaknk you!

Please reward points if helpful.

Sunmit.

Former Member
0 Kudos
144

Hi

Type doesnot occupy any memory tht means they r just like structure we use in our prog where as like has its own memory and also if we define using like it takes all the properties of tht data object but same is not the case with type.

Hope this helps u.

Regards,

Seema.

Former Member
0 Kudos
144

Type Vs Like

-


Type :

This can be Created only if you have declared a correponding Object using TYPES.

The Occupation of Memory is Dynamic i.e the memory is occupied only when it is used.

Like :

The can be Creatde only if you have declared the object is available globally or an local object.

The Occupation of Memory is Static i.e this occupies the same memory as the correpondin one.

Correct if m Wrong

Message was edited by: Ramachandran Sathyanarayanan