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 DATA TYPE and DATA OBJECT & differences b/w TYPE and LIKE

Former Member
0 Kudos

hai

can any one say the differences between Data type and Data Object.

And also differences between TYPE and LIKE

thanks

Gani

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Data type cant hold any value, but object has value.

Types is used to Create data types.

eg, types: abc type i,

Now abc is type.

U can also declare one more variable as

Data: cvb type abc.

Reward Points if useful

Regards,

Nishant

7 REPLIES 7

Former Member
0 Kudos

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

Data types

Data types are the actual type definitions in the ABAP Dictionary. They allow you to define elementary types, reference types, and complex types that are visible globally in the system. The data types of database tables are a subset of all possible types, namely flat structures.

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:

&#65533; A predefined ABAP type to which you refer using the TYPE addition

&#65533; An existing local data type in the program to which you refer using the TYPE addition

&#65533; The data type of a local data object in the program to which you refer using the LIKE addition

&#65533; 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.

&#65533; 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.

&#65533; 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.

Regards

Former Member
0 Kudos

Hi Spider,

Data is used to create data object ( means memory will be allocated)

Types is used to create structures

Type is used to refer to Predefined data types ( like n,i,f,d,t,p,x....)

Like is used to refer the local variables.

All ABAP programs can define their own data types. Within a program, procedures can also define local types.

You define local data types in a program using the

TYPES 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 DECIMALS dec...

TYPES|DATA var TYPE type LENGTH len DECIMALS dec...

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

kindly reward if helpful,

cheers,

hema.

Former Member
0 Kudos

Also this will help u spider,

Please have a look below .Hope it is suitable and simpler solution for your question.

Please do reward if useful.

Thankx.

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

TYPES dtype LIKE dobj ...

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

DATA var {LIKE dobj} ...

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

&#65533; Construction of reference types

REF TO type|dobj

&#65533; Construction of structured data types

BEGIN OF struc_type.

...

END OF struc_type.

&#65533; Construction of table types

tabkind OF linetype WITH key

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:

&#65533; A predefined ABAP type to which you refer using the TYPE addition

&#65533; An existing local data type in the program to which you refer using the TYPE addition

&#65533; The data type of a local data object in the program to which you refer using the LIKE addition

&#65533; 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.

&#65533; 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.

&#65533; 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.

cheers,

hema.

Kanagaraja_L
Active Contributor
0 Kudos

Hi Gani,

DATA TYPE is.....the type of a DATA OBJECT,

Data object will have memory and Data type will not have any memory.

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.

Kanagaraja L

Former Member
0 Kudos

Hi,

Data types

Data types are templates for creating data objects. Data types can be defined independently in the ABAP program or in the ABAP Dictionary. As attributes of a data object, data types can also exist in a non-independent state. Data types do not use any memory space for work data, but may require memory for administration information.

Data objects

A data object is an instance of a data type and occupies as much memory space as its type specifies. An ABAP program only works with data that is available as content of data objects. Data objects are either created implicitly as named data objects, or exanonymous data objects using CREATEDATA.

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.

Former Member
0 Kudos

hi,

_Data Types and Data Objects_

Programs work with local program data – that is, with byte sequences in the working memory. Byte sequences that belong together are called fields and are characterized by a length, an identity (name), and – as a further attribute – by a data type. All programming languages have a concept that describes how the contents of a field are interpreted according to the data type.

In the ABAP type concept, fields are called data objects. Each data object is thus an instance of an abstract data type. There are separate name spaces for data objects and data types. This means that a name can be the name of a data object as well as the name of a data type simultaneously.

Data Types

As well as occurring as attributes of a data object, data types can also be defined independently. You can then use them later on in conjunction with a data object. The definition of a user-defined data type is based on a set of predefined elementary data types. You can define data types either locally in the declaration part of a program using the TYPESstatement) or globally in the ABAP Dictionary. You can use your own data types to declare data objects or to check the types of parameters in generic operations.

All programming languages distinguish between various types of data with various uses, such as ….. type data for storing or displaying values and numerical data for calculations. The attributes in question are described using data types. You can define, for example, how data is stored in the repository, and how the ABAP statements work with the data.

Data types can be divided into elementary, reference, and complex types.

a. Elementary Types

These are data types of fixed or variable length that are not made up of other types.

The difference between variable length data types and fixed length data types is that the length and the memory space required by data objects of variable length data types can change dynamically during runtime, and that these data types cannot be defined irreversibly while the data object is being declared.

Predefined and User-Defined Elementary Data Types

You can also define your own elementary data types in ABAP using the TYPES statement. You base these on the predefined data types. This determines all of the technical attributes of the new data type. For example, you could define a data type P_2 with two decimal places, based on the predefined data type P. You could then use this new type in your data declarations.

b. Reference Types

Reference types are deep data types that describe reference variables, that is, data objects that contain references. A reference variable can be defined as a component of a complex data object such as a structure or internal table as well as a single field.

c. Complex Data Types

Complex data types are made up of other data types. A distinction is made here between structured types and table types.

Data Objects

Data objects are the physical units with which ABAP statements work at runtime. The contents of a data object occupy memory space in the program. ABAP statements access these contents by addressing the name of the data object and interpret them according to the data type.. For example, statements can write the contents of data objects in lists or in the database, they can pass them to and receive them from routines, they can change them by assigning new values, and they can compare them in logical expressions.

Each ABAP data object has a set of technical attributes, which are fully defined at all times when an ABAP program is running (field length, number of decimal places, and data type). You declare data objects either statically in the declaration part of an ABAP program (the most important statement for this is DATA), or dynamically at runtime (for example, when you call procedures). As well as fields in the memory area of the program, the program also treats literals like data objects.

A data object is a part of the repository whose content can be addressed and interpreted by the program. All data objects must be declared in the ABAP program and are not persistent, meaning that they only exist while the program is being executed. Before you can process persistent data (such as data from a database table or from a sequential file), you must read it into data objects first. Conversely, if you want to retain the contents of a data object beyond the end of the program, you must save it in a persistent form.

Declaring Data Objects

Apart from the interface parameters of procedures, you declare all of the data objects in an ABAP program or procedure in its declaration part. These declarative statements establish the data type of the object, along with any missing technical attributes. This takes place before the program is actually executed. The technical attributes can then be queried while the program is running.

The interface parameters of procedures are generated as local data objects, but only when the procedure is actually called. You can define the technical attributes of the interface parameters in the procedure itself. If you do not, they adopt the attributes of the parameters from which they receive their values.

ABAP contains the following kinds of data objects:

a. Literals

Literals are not created by declarative statements. Instead, they exist in the program source code. Like all data objects, they have fixed technical attributes (field length, number of decimal places, data type), but no name. They are therefore referred to as unnamed data objects.

b. Named Data Objects

Data objects that have a name that you can use to address the ABAP program are known as named objects. These can be objects of various types, including text symbols, variables and constants.

Text symbols are pointers to texts in the text pool of the ABAP program. When the program starts, the corresponding data objects are generated from the texts stored in the text pool. They can be addressed using the name of the text symbol.

Variables are data objects whose contents can be changed using ABAP statements. You declare variables using the DATA, CLASS-DATA, STATICS, PARAMETERS, SELECT-OPTIONS, and RANGESstatements.

Constants are data objects whose contents cannot be changed. You declare constants using the CONSTANTSstatement.

c. Anonymous Data Objects

Data objects that cannot be addressed using a name are known as anonymous data objects. They are created using the CREATE DATAstatement and can be addressed using reference variables.

d. System-Defined Data Objects

System-defined data objects do not have to be declared explicitly - they are always available at runtime.

e. Interface Work Areas

Interface work areas are special variables that serve as interfaces between programs, screens, and logical databases. You declare interface work areas using the TABLES and NODESstatements.

What is the difference between Type and Like?

Answer1:

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.

Answer2:

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.

Answer3:

type refers the existing data type

like refers the existing data object

reward if useful

thanks and regards

suma sailaja pvn

Former Member
0 Kudos

Data type cant hold any value, but object has value.

Types is used to Create data types.

eg, types: abc type i,

Now abc is type.

U can also declare one more variable as

Data: cvb type abc.

Reward Points if useful

Regards,

Nishant