- Tech know how online

Initialization and assignment semantics

Not all programming languages offer the possibility to initialize newly created variables - with default values or user-controlled. However, objects must always be in a defined state after their creation, i.e. the provision of a reserved memory area. They must still be assigned an initial value before they are used. However, assignment and initialization are two different operations, which can also be handled differently for user-defined types (i.e.: classes).

Initialization is the assignment of an initial value within an object definition. It is to be noted that also the assignment of current parameters to the formal parameters of a method corresponds to the initialization, the formal parameters as local objects. Also the return of objects as function value must be treated like the initialization and has therefore the same semantics. Assignment is the application of the assignment operator to corresponding arguments in expressions - that is, not in definitions.

Different semantics exist for both the initialization and assignment operations for user-defined types.

Value semantics: assignment involves copying all components of the assigned object bit by bit. Here, copying references means just copying their values - in the case of object references, just the object identities - and not copying the contents of the referenced values, i.e., the state of the objects.

Reference semantics: as before, but for reference values, the referenced value is copied to the assigned object.

Thus, in object-oriented software, a distinction must be made between whether an object identity is to be copied by means of value semantics, e.g., in order to make the same object usable several times in different places, and whether the state of an object is to be assigned to another object by means of reference semantics, i.e., whose value assignment is determined by dereferencing, if necessary recursively, e.g., in the case of independent subobjects. In the applications, in which both cases are meaningful and both semantics are realizable, also appropriate differently implemented method must be offered for these forms of the assignment.

Englisch: Initialization and assignment semantics
Updated at: 07.12.2009
#Words: 335
Links: default, user, reserved memory, area, initialization
Translations: DE

All rights reserved DATACOM Buchverlag GmbH © 2024