Data type
From DocForge
A data type is a set of values and the operations on those values in a programming language. For example, the Java "int" type is the set of 32-bit integers together with the operations "+", "*", "%", etc. that operate over integers. A data type can also be thought of as 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 another 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 2007, 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 2^32-1 or signed integer values from -2^31 to 2^31-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
[edit] Integer Numbers
An integer number can hold a whole number, but no fraction. Commas can not be used as part of the number as this would generate a syntax error. Examples of integer numbers:
- 42
- -233000
[edit] Real Numbers
A real number can hold a whole number or a fractional number that uses a decimal point. Examples of real numbers:
- 20.0005
- -5000.12
[edit] Strings
String data is used to store characters and words. The string data can include numbers and other numerical symbols but will be treated as text. Examples of strings are:
- "A"
- "Hello World"
- "I am 600 years old"
- "1.2.3.4.5.6.7.8.9"
- ""
- " "
[edit] Numeric Data Type Ranges
Each numeric data type has a maximum and minimum value known as the range. It is important to know the ranges, especially when dealing with smaller types, as you can only store numbers that lie within that range. Attempting to store a number outside the range may lead to compiler/runtime errors, or to incorrect calculations (due to truncation) depending on the language being used.
The range of a variable is based on the number of bytes used to save the value, and an integer data type is usually able to store 2^n values (where n is the number of bits). For other data types (e.g. floating point values) the range is more complicated and will vary depending on the method used to store it. There are also some types that do not use entire bytes, e.g. a boolean that requires a single bit, and represents a binary value (although in practice a byte is often used, with the remaining 7 bits being redundant). Some programming languages also allow the opposite direction, that is, the programmer defines the range and precision needed to solve a given problem and the compiler chooses the most appropriate integer or floating point type automatically.
The following table lists a set of common of numeric data types and their ranges. Note that the size of most data types may vary between platforms and languages. The values listed here are the most commonly used sizes in use today.
| Data Type | Size | Range |
|---|---|---|
| Integer types | ||
| Boolean | 1 bit (though often stored as 1 byte) | 0 to 1 |
| Byte | 8 bits | 0 to 255 |
| Word | 2 bytes | 0 to 65535 |
| Double Word | 4 bytes | 0 to 4,294,967,295 |
| Integer | 4 bytes | –2,147,483,648 to 2,147,483,647 |
| Double Integer | 8 bytes | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| Floating point types | ||
| Real | 4 bytes | 1E-37 to 1E+37 (6 decimal digits) |
| Double Float | 8 bytes | 1E-307 to 1E+308 (15 decimal digits) |

