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:
“Maybe we were the blind mechanics of disaster, but you dont pin the guilt on the scientists that easily. You might as well pin it on M motherhood.... Every man who ever worked on this thing told you what would happen. The scientists signed petition after petition, but nobody listened. There was a choice. It was build the bombs and use them, or risk that the United States and the Soviet Union and the rest of us would find some way to go on living.”
—John Paxton (19111985)