In computer science, datatype (often simply called type) is a concept that clarifies contexts for numerical and other operations, limits destructive and useless operations and provides semantic documentation to programming languages. Types are applied both immediate values[?] and variables. They are loosely related to similar notions in mathematics and logic.
Types are usually associated either with values in memory or with objects such as variables[?]. Because any value is simply a set of bits for computers, there is no distinction in hardware even among memory addresses[?], instruction code[?], characters, integers and floating-point numbers. Numerical and string constants and expression in code can and often do imply type in a particular context. For example, an expression 3.14
implies its type is floating-point while [1, 2, 3] implies type is a list of integers; typically an array.
In some languages, such as C and Java, some types are associated with the particular implementation. For example, in Java, type int
is defined as a 4-byte signed integer. On the other hands, some languages only define the semantic behavior of all types.
The type system allows operations to be done relying on contexts by type. For example, in an arithmetic expression, a + b
, if a and b are typed as integer, an underlying operation can be integer addition. If the type is real, floating-point addition is probably done. In generics the type of values determines which code will be executed.
Types make impossible to code some operations which cannot be valid in certain context. This mechanism effectively catches the majority of common mistakes made by programmers. For example, an expression "Hello, Wikipedia" / 3
is invalid because a string literal cannot be divided by an integer in the usual sense.
Using types in languages also improves documentation of code. For example, the declaration of a variable as being of a specific type documents how the variable is used. In fact, many languages allow programmers to define semantic types derived from builtin types; either composed of elements of one or more builtin types, or simply as aliases for names of builtin types.
While some languages use types during compile-time and don't have them during run-time, type information can be stored in memory for use during run-time. Many OOP languages keep certain information about type at run-time to make possible dynamic binding[?].
Some, particularly script languages and assembly languages provide no type. Object-oriented programming languages mostly support a type system by classes.
The process of verifying types is called type checking[?]. If it occurs at compile-time, the whole type system is called statically typed. If it occurs at run-time, the type system is called dynamically typed. C, Java and Pascal are statically typed while most script languages, including Perl, Ruby and Python, are dynamically typed. One of the primary tasks of semantic analysis is type checking[?]. In dynamic scope, type checking must be done at run-time because variables can be differently typed according to execution path.
If type A is compatible with type B, A is a subtype of B while not always vice versa. See subtype for detail.
Types are generally divided into primitive types[?] (also called built-in types or basic types) and composite types[?]. Typical primitive types[?] include integer and floating-point number. In some programming languages, strings are also a primitive type. Composite types[?], as the name implies, are types consisting of basic types. They are also called a record or structure type.
Datatypes are often of first-class, second-class or third-class value.
Search Encyclopedia
|
Featured Article
|