Difference between revisions of "Data type"
m (Added link) |
m (Added links) |
||
| Line 10: | Line 10: | ||
==Strings== | ==Strings== | ||
| − | Sometimes called "character strings", strings represent textual data.<ref>[http://www.regular-expressions.info/tutorial.html]</ref> Computer hardware is typically not optimized for the types of operations that programmers like to do, such as concatenating or inserting, because these operations can make strings change size, requiring moving large blocks of memory around. Many ways of dealing with this problem have been devised over the last 60 years, involving efficiency vs. naturalness trade-offs. Some languages, like C and C++, permit efficient string manipulation, at a cost of programmer naturalness and understandability. Others, like | + | Sometimes called "character strings", strings represent textual data.<ref>[http://www.regular-expressions.info/tutorial.html]</ref> Computer hardware is typically not optimized for the types of operations that programmers like to do, such as concatenating or inserting, because these operations can make strings change size, requiring moving large blocks of memory around. Many ways of dealing with this problem have been devised over the last 60 years, involving efficiency vs. naturalness trade-offs. Some languages, like [[C (programming language)|C]] and [[C++]], permit efficient string manipulation, at a cost of programmer naturalness and understandability. Others, like [[Lisp]] and Perl, permit natural use of strings at a cost of efficiency. [[SQL]] is sort of between the two. |
| − | The representation of the text is dependent upon the encoding method. For instance, a [[Unicode|Unicode-16]] string will use two bytes per ''character'', whereas a string encoded with the ANSI ([[ASCII]]) standard represents one character per byte. A [[UTF-8]] encoded string uses a variable number of bytes per character. The encoding indicates how the bits in each character translate to a specific glyph. For instance, the byte value of 65 indicates the uppercase "A" character in the Ansi and UTF-8 encoding. The maximum size of string data is language- and implementation-dependent. | + | The representation of the text is dependent upon the [[Character encoding|encoding]] method. For instance, a [[Unicode|Unicode-16]] string will use two bytes per ''character'', whereas a string encoded with the ANSI ([[ASCII]]) standard represents one character per byte. A [[UTF-8]] encoded string uses a variable number of bytes per character. The encoding indicates how the bits in each character translate to a specific glyph. For instance, the byte value of 65 indicates the uppercase "A" character in the Ansi and UTF-8 encoding. The maximum size of string data is language- and implementation-dependent. |
==Pointers== | ==Pointers== | ||
Revision as of 21:40, March 24, 2021
A data type is essentially a rule for how the underlying bits of a value are interpreted. Because of this, the number of data types is practically unlimited. However, there are certain basic data types that are provided by most programming languages. These can be grouped into boolean, numeric, string, and pointer.
Booleans
Boolean values represent one of two values: true and false. As such, they can be represented by a single bit. However, most compilers use the native word size worth of bits to represent a single boolean value. The interpretation of the bits varies somewhat between different languages, but typically if the least significant bit is set to 0, the value is interpreted as false. If the least significant bit is 1, the value is interpreted as true.
Numerics
Numeric values represent numbers. Most programming languages provide both integer and real data types. Further, many languages support several sizes of both types of numbers. The default integer size usually depends upon the underlying hardware. On a 32-bit CPU, the default integer size will also be 32 bits. It is typical that the compiler will support a maximum integer size that is twice the underlying hardware word size (for instance, 64-bit integers on a 32-bit CPU), and a minimum integer size of 1 byte. Integers can be signed, or unsigned. Signed integers use the most significant bit as the sign (set to indicate negative, clear to indicate positive), thus a 16-bit signed integer can represent a number between -32768 and 32767, inclusive. An unsigned 16-bit integer can represent a number between 0 and 65535. Most languages use 2s-complement encoding for interpreting the underlying bits.
In a like manner, floating point values come in various sizes, depending upon the floating-point standard that is used. Typically single precision represents a 32-bit floating-point value, and double precision represents a 64-bit value.
Strings
Sometimes called "character strings", strings represent textual data.[1] Computer hardware is typically not optimized for the types of operations that programmers like to do, such as concatenating or inserting, because these operations can make strings change size, requiring moving large blocks of memory around. Many ways of dealing with this problem have been devised over the last 60 years, involving efficiency vs. naturalness trade-offs. Some languages, like C and C++, permit efficient string manipulation, at a cost of programmer naturalness and understandability. Others, like Lisp and Perl, permit natural use of strings at a cost of efficiency. SQL is sort of between the two.
The representation of the text is dependent upon the encoding method. For instance, a Unicode-16 string will use two bytes per character, whereas a string encoded with the ANSI (ASCII) standard represents one character per byte. A UTF-8 encoded string uses a variable number of bytes per character. The encoding indicates how the bits in each character translate to a specific glyph. For instance, the byte value of 65 indicates the uppercase "A" character in the Ansi and UTF-8 encoding. The maximum size of string data is language- and implementation-dependent.
Pointers
A pointer represents an address in memory. This allows indirection. The memory address indicated by the pointer typically contains data of a specific type. For instance, a pointer to an integer.
Class types
Class types define objects. Variables of this type contain instances of the specific class, or a descendant thereof. Used in Object Oriented programming.
Structures
Structures, or records, are collections of multiple named data types.
Collections
Collections represent a group of variables of the same data type. The organization of the collection depends upon the type of collection. Typical collections natively supported by programming languages include arrays, but also can include trees, lists, queues, stacks, etc. Some languages provide iterators which provide a means of processing through each item in the collection.
Enumerations
Enumerations are user-defined identifiers that are used in place of numeric or string constants.