Data type

From Wikipedia, the free encyclopedia

A data type is a constraint placed upon the interpretation of data in a type system in computer programming. Common types of data in programming languages include primitive types (such as integers, floating point numbers or characters), tuples, records, algebraic data types, abstract data types, reference types, classes and function types. A data type describes representation, interpretation and structure of values manipulated by algorithms or objects stored in computer memory or other storage device. The type system uses data type information to check correctness of computer programs that access or manipulate the data.

Contents

[edit] Machine data types

All data in computers based on digital electronics is represented as bits (alternatives 0 and 1) on the lowest level. The smallest addressable unit of data is a group of bits called a byte (usually an octet, which is 8 bits). The unit processed by machine code instructions is called a word (as of 2006, typically 32 or 64 bits). Most instructions interpret the word as a binary number, such that a 32-bit word can represent unsigned integer values from 0 to 232 − 1 or signed integer values from − 231 to 231 − 1. Because of two's complement, the machine language and machine don't need to distinguish between these unsigned and signed data types for the most part.

There is a specific set of arithmetic instructions that use a different interpretation of the bits in word as a floating-point number.

[edit] Primitive data types

Main article: primitive type

Programming languages provide some primitive data types as basic building blocks for programs and more specialized composite types. Typically primitive data types include various integer, floating-point and string types. Although basic building blocks, arrays, records and references for relations between pieces of data might not be included in primitive data types as they can be seen the collection of several primitive values.

[edit] Number types

Main article: Numerical tower

[edit] Composite types

Main article: Composite type

These can include the following (depending on the programming language):

[edit] Integer Numbers

An integer number can hold a whole number, but no fraction. For the value to be negative, you must place a hyphen symbol (-) before the value. You must not use commas as part of the number as this would generate a syntax error. Examples of integer numbers:

  • 42
  • 10000
  • -233000
  • -100

[edit] Real Numbers:

A real number can hold a whole number or a fractional number that uses a decimal point. For the value to be negative, you must place a hyphen symbol (-) before the value. Examples of real numbers:

  • 20.0005
  • 99.9
  • -5000.12
  • -9999.9991

[edit] Strings:

String data is non-numerical, and is used to store characters and words. All strings consist of characters enclosed within double quotation marks. The string data can include numbers and other numerical symbols but will be treated as text. Examples of strings are:

  • "A"
  • "Hello World"
  • "Telephone"
  • "I am 99 years old"
  • "1.2.3.4.5.6.7.8.9"

Each string can consist of as many characters as the memory allows. You can also have a string with no data whatsoever, represented by an empty pair of double quotation marks.

[edit] Data type ranges:

Each numeric datatype has a maximum and minimum value known as the range. It is important to know these ranges, especially when dealing with smaller datatypes. This range depend of the number of bytes used to save the value, and can be found by using the following formula, where n the number of bytes used and r is the number of combinations that can be expressed:

r = 28n

There are some types that do not use entire bytes, this is the case a boolean, that uses a single bit, and represents a binary value.

The following table lists a set of common of numeric datatypes and their ranges:

Data Type Bits Range
Boolean 1 0 to 1
Byte 8 0 to 255
Word 8 * 2 0 to 65535
Double Word 8 * 4 0 to 4,294,967,295
Integer 8 * 4 –2,147,483,648 to 2,147,483,647
Double Integer 8 * 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Real 8 * 4 1E-37 to 1E+37 (6 decimal digits)
Double Float 8 * 8 1E-307 to 1E+308 (15 decimal digits)

[edit] Data structures

Main article: Data structure

[edit] Abstract types

Main article: Abstract type

[edit] See also

  • Type theory for the mathematical models of types
  • Type system for different choices in programming language typing

[edit] References

  • Luca Cardelli, Peter Wegner. On Understanding Types, Data Abstraction, and Polymorphism, [1] from Computing Surveys, (December, 1985)