Primitive type

Primitive type

In computer science, primitive types — as distinct from "composite types" — are data types provided by a programming language as basic building blocks. Primitive types are also known as "built-in types" or "basic types".

Depending on the language and its implementation, primitive types may or may not have a one-to-one correspondence with objects in the computer's memory. However, one usually expects operations on primitive types to be the fastest language constructs there are. Integer addition, for example, can be performed as a single machine instruction, and some processors offer specific instructions to process sequences of characters with a single instruction. In particular, the C standard mentions that "a 'plain' int object has the natural size suggested by the architecture of the execution environment". This means that int is likely to be 32 bits long on a 32-bit architecture. Primitive types are value types.

Most languages do not allow the behaviour or capabilities of primitive types to be modified by programs. Exceptions include Smalltalk, which permits primitive datatypes to be extended within a program, adding to the operations that can be performed on them or even redefining the built-in operations.

Overview

The actual range of primitive types that is available is dependent upon the specific programming language that is being used. For example, in C, strings are a composite data type, whereas in modern dialects of Basic and in JavaScript, they are built-in and assimilated to a primitive data type.

Typical primitive types may include:
* Character (character, char);
* Integer (integer, int, short, long, byte) with a variety of precisions;
* Floating-point number (float, double, real, double precision);
* Fixed-point number (fixed) with a variety of precisions and a programmer-selected scale.
* Boolean having the values true and false.
* Reference (also called a "pointer" or "handle"), a small value referring to another object's address in memory, possibly a much larger one.

More sophisticated types which can be primitive include:
* Tuples in ML, Python
* Linked lists in Lisp
* Complex numbers in Fortran, C (C99), Lisp, Python
* Rational numbers in Lisp
* Hash tables in various guises, in Lisp, Perl, Python, Lua
* First class functions, closures, continuations in Functional programming languages such as Lisp and ML

Specific primitive types

Integer numbers

An integer number can hold a whole number, but no fraction. Integers may be either signed (allowing negative values) or unsigned (nonnegative values only). Typical sizes of integers are:

Literals for integers consist of a sequence of digits. Most programming languages disallow the use of commas for digit grouping, although FORTRAN (77 and Fortran 90 and above fixed form source but not free form source) allows embedded spaces, and Perl, Ruby, and D allow embedded underscores. Negation is indicated by a minus sign (−) before the value. Examples of integer literals are:

* 42
* 10000
* −233000

Booleans

A boolean type, typically denoted "bool" or "boolean", is a single-bit type that can be either "true" (1) or "false" (0). In some languages (e.g., C++), bools may be implicitly converted to integers (for example, "true + true" is a valid expression equal to 2), but other languages (e.g., Java and Pascal) disallow this.

Floating-point numbers

A floating-point number represents a real number that may have a fractional part. These numbers are stored internally in scientific notation, typically in binary but sometimes in decimal. Because floating-point numbers have only a limited number of digits, most values can be represented only approximately.

Many languages have both a single precision (often called "float") and a double precision type.

Literals for floating point numbers include a decimal point, and typically use "e" to denote scientific notation. Examples of floating-point literals are:

* 20.0005
* 99.9
* −5000.12
* 6.02e23

Some languages (e.g., FORTRAN) also have a complex number type comprising two floating-point numbers: a real part and an imaginary part.

Fixed-point numbers

A fixed-point number represents a real number that may have a fractional part. These numbers are stored internally in a scaled-integer form, typically in binary but sometimes in decimal. Because fixed-point numbers have only a limited number of digits, most values can be represented only approximately. Because fixed-point numbers have a limited range of values, the programmer must be careful to avoid overflow in intermediate calculations as well as the final results.

Characters and strings

A character type (typically called "char") may contain a single letter, digit, punctuation mark, or control character. Some languages have two character types, a single-byte type for ASCII characters and a multi-byte type for Unicode characters.

Characters may be combined into strings. The string data can include numbers and other numerical symbols but will be treated as text.

In most languages, a string is equivalent to an array of characters, but Java treats them as distinct types. Other languages (such as Python, and many dialects of BASIC) have no separate character type, but only strings with a length of one.

Literals for characters and strings are usually surrounded by quotation marks: often, single quotes (') are used for characters and double quotes (") are used for strings.

Examples of character literals in C syntax are:
* 'A'
* '4'
* '$'
* ' ' (tab character)

Examples of string literals in C syntax are:
* "A"
* "Hello World"
* "I am 6000 years old"

Numeric data type ranges

Each numeric data type has a maximum and minimum value known as the 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 [There are situations where one or more bits are reserved for other functions, e.g. parity checking.] 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 (such as Ada and Pascal) 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.

See also

* Primitive wrapper class
* Object type

References


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • primitive type — noun A data type provided by a programming language as a basic building block, such as integer, character or Boolean. Ant: composite type See Also: built in type …   Wiktionary

  • Type inference — Type inference, or implicit typing, refers to the ability to deduce automatically the type of a value in a programming language. It is a feature present in some strongly statically typed languages. It is often characteristic of but not limited to …   Wikipedia

  • Primitive — is a subjective label used to imply that one thing is less sophisticated or less advanced than some other thing. Being a comparative word it is also relative in nature.Indigenous peoples and their beliefs and practices are sometimes described as… …   Wikipedia

  • Primitive wrapper class — A primitive wrapper class in the Java programming language is one of eight classes provided in the Javadoc:SE|package=java. ja. package] to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are… …   Wikipedia

  • Primitive (album) — Infobox Album | Name = Primitive Type = Album Artist = Soulfly Released = September 26, 2000 Recorded = The Saltmine Studio Oasis in Mesa, Arizona Genre = Alternative metal, nu metal Length = 52:16 Label = Roadrunner Producer = Toby Wright Max… …   Wikipedia

  • Primitive (homonymie) — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom …   Wikipédia en Français

  • Type system — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Primitive accumulation of capital — is a concept introduced by Karl Marx in part 8 of the first volume of Das Kapital (in German: ursprüngliche Akkumulation , literally original accumulation or primeval accumulation ). Its purpose is to help explain how the capitalist mode of… …   Wikipedia

  • Primitive markings — among domestic horses are a group of hair coat markings and qualities associated with primitive breeds,cite journal |last=Stachurska |first=AM |title=Inheritance of primitive markings in horses |journal=J. Anim. Breed. Genet. |year=1999… …   Wikipedia

  • type — n 1 *symbol, emblem, attribute Analogous words: *sign, mark, token: intimation, suggestion (see corresponding verbs at SUGGEST 1): adumbration, shadowing (see corresponding verbs at SUGGEST 2) Antonyms: antitype 2 Type, kind, sort, stripe …   New Dictionary of Synonyms

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”