Union (computer Science)
In computer science, a union is a value that may have any of several representations or formats; or a data structure that consists of a variable which may hold such a value. Some programming languages support special data types, called union types, to describe such values and variables. In other words, a union type definition will specify which of a number of permitted primitive types may be stored in its instances, e.g. "float or long integer". Contrast with a record, which could be defined to contain a float and an integer; whereas, in a union, there is only one value at a time.
You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.
In type theory, a union has a sum type.
Depending on the language and type, a union value may be used in some operations, such as assignment and comparison for equality, without knowing its specific type. Other operations may require that knowledge, either by some external information, or by the use of a tagged union.
Note: The remainder of this article refers strictly to primitive untagged unions, as opposed to tagged unions.
Because of the limitations of their use, untagged unions are generally only provided in untyped languages or in an unsafe way (as in C). They have the advantage over simple tagged unions of not requiring space to store the tag.
The name "union" stems from the type's formal definition. If one sees a type as the set of all values that that type can take on, a union type is simply the mathematical union of its constituting types, since it can take on any value any of its fields can. Also, because a mathematical union discards duplicates, if more than one field of the union can take on a single common value, it is impossible to tell from the value alone which field was last written.
However, one useful programming function of unions is to map smaller data elements to larger ones for easier manipulation. A data structure, consisting for example of 4 bytes and a 32-bit integer, can form a union (in this case with an unsigned 64-bit integer) and thus be more readily accessed for purposes of comparison etc.
Like a structure, all of the members of a union are by default public. The keywords private, public, and protected may be used inside a struct or a union in exactly the same way they are used inside a class for defining private, public, and protected members.
Read more about Union (computer Science): Syntax and Example, Difference Between Union and Structure
Famous quotes containing the word union:
“These semi-traitors [Union generals who were not hostile to slavery] must be watched.Let us be careful who become army leaders in the reorganized army at the end of this Rebellion. The man who thinks that the perpetuity of slavery is essential to the existence of the Union, is unfit to be trusted. The deadliest enemy the Union has is slaveryin fact, its only enemy.”
—Rutherford Birchard Hayes (18221893)