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 & like

prasanth_kasturi
Active Contributor
0 Likes
1,136

Plz get me the differences between 'like' and 'type' statements

9 REPLIES 9
Read only

Former Member
0 Likes
1,066

Hi,

see this thread.

rgds,

bharat.

Read only

Former Member
0 Likes
1,066

Read only

Former Member
0 Likes
1,066

Hi Prasanth,

Here is a simple main difference ..

Type : it will allocate memory during execution (object type).

Like : it will allocate memory immediatly.

---type will improve performance.

---p is packed type wherein we can restrict decimal values.

var type p decimals 2.

---float can hold more value than packed data type

Reward if helpful.

Thankyou,

Regards.

Read only

Former Member
0 Likes
1,066

Hi Prasanth,

TYPE keyword is used to just define a field with same type of field specified (character, numeric etc)..It will not associate any of the attribute of target field.

f LIKE f1 --

LIKE : Field f is created with the same field attribtues as the data object f1, which has already been declared.

Example, if f1 has value range defined in it's domain, f will also get same value range associated with it ( of course f will have same type as f1)

Regards,

Mohaiyuddin

Read only

Former Member
0 Likes
1,066

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

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

Rewards if useful.

Read only

Former Member
0 Likes
1,066

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.

for defining data types and specifying the types of interface parameters or field symbols. The addition

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

ABAP Statements with LIKE References

Definition of local types in a program using

TYPES in the program, then in the ABAP Dictionary for a database table or flat structure with the same name. You can no longer use this kind of type reference in ABAP Objects classes. You should also avoid using the LIKE addition in other ABAP programs except to refer to data objects. To refer to data types, you should use the TYPE addition instead.

TYPE

Each ABAP program define its own data types using the statement.

TYPES dtype TYPE type ...

and declare its own variables or instance attributes of classes using the statement

DATA var {TYPE type} ...

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 object

· 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

Constructing New Data Types

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

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

Edited by: shylaja puchala on Jan 9, 2008 6:29 AM

Read only

Former Member
0 Likes
1,066

Hi,

TYPE is used while refering to the data types and types declared using types statement, where as LIKE is used to refer to the data objects.

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

TYPE means it is a predefined data type.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.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

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.

Reward if helpful.

Regards,

Harini.S

Read only

Former Member
0 Likes
1,066

Hi,

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.

regards,

vasavi.

reward if helpful.

Read only

Former Member
0 Likes
1,066

Hi,

The Statements TYPES and DATA

Each ABAP program define its own data types using the statement.

TYPES dtype TYPE type ...

and declare its own variables or instance attributes of classes using the statement

DATA var {TYPE type} ...

Within the program or a class, you can also define local data types and variables within procedures. Local variables in procedures obscure identically-named variables in the main program or class.

When creating data types and data objects, there are a number of naming convention that also apply for other local program definitions, such as procedures. These are described in detail in the keyword documentation.

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

Constructing New Data Types

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

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

· Construction of reference types

REF TO type|dobj

· Construction of structured data types

BEGIN OF struc_type.

...

END OF struc_type.

· Construction of table types

tabkind OF linetype

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

Referring to Known Data Types or Data Objects

Using the additions TYPE or LIKE in the TYPESstatement, local data types in a program can be referred to known data types or data objects. This is mainly the case with user-defined elementary data types. If you declare variables using the additions TYPE type or LIKE dobj with statement DATA, the data type of var is already fully defined before the declaration is made.

The known types or data that are referred to must be visible at the point where the data type or variable is declared.

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.

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.

Referring to Generic Data Types

If you refer to one of the generic predefined ABAP types of fixed length (c, n, p, x) in the TYPES or DATA statement, you must specify the undefined technical attributes.

TYPES|DATA var[(length)] TYPE type ...

TYPES|DATA var TYPE type ...

DATA: text1,

text2 LENGTH 2,

text3 TYPE c LENGTH 3,

pack TYPE p DECIMALS 2 VALUE '1.225'.

This example creates three character variables with field lengths of one, two, and three bytes respectively, and a packed number variable with field length 8 bytes and two decimal places. If the attribute Fixed point arithmetic is set, the value of pack is 1.23.

This example shows how to declare elementary data objects with reference to predefined ABAP types.

PROGRAM demo_elementary_data_objects.

DATA text1 TYPE c LENGTH 20.

DATA text2 TYPE string.

DATA number TYPE i.

text1 = 'The number'.

number = 100.

text2 = 'is an integer.'.

WRITE: text1, number, text2.

This program produces the following output on the screen:

The number 100 is an integer.

In this example, the data objects text1, text2 and number are declared with the DATA statement. The technical attributes are determined by referring to the predefined ABAP types c, string, and I. Values from unnamed literals are assigned to the data objects. The contents of the named data objects are displayed on the list.

Specifying a Start Value

When you declare an elementary fixed-length variable, the DATAstatement automatically fills it with the type-specific initial value as listed in the table in the Predefined ABAP Types section.

However, you can also specify a starting value of a fixed-length elementary variable (also within a structure declaration) using the VALUE addition in the DATAstatement:

DATA var ... VALUE val|{IS INITIAL}.

Specifying start values:

DATA: counter TYPE p VALUE 1,

date TYPE d VALUE '19980601',

flag TYPE n VALUE IS INITIAL.

After this data declaration, the character string flag contains its type specific

Initial value ‘0’.

Regards,

Renjith Michael.