Template:Short description

In computer science, primitive data types are a set of basic data types from which all other data types are constructed.<ref>Template:Cite book</ref> Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary.<ref>Template:Cite book</ref> More generally, primitive data types may refer to the standard data types built into a programming language (built-in types).<ref name=Khurana>Template:Cite book</ref><ref>Template:Cite book</ref> Data types which are not primitive are referred to as derived or composite.<ref name=Khurana/>

Primitive types are almost always value types, but composite types may also be value types.<ref>Template:Cite book</ref>

Common primitive data typesEdit

The most common primitive types are those used and supported by computer hardware, such as integers of various sizes, floating-point numbers, and Boolean logical values. Operations on such types are usually quite efficient. Primitive data types which are native to the processor have a one-to-one correspondence with objects in the computer's memory, and operations on these types are often the fastest possible in most cases.<ref name="Agner">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Integer addition, for example, can be performed as a single machine instruction, and some offer specific instructions to process sequences of characters with a single instruction.Template:Fact But the choice of primitive data type may affect performance, for example it is faster using SIMD operations and data types to operate on an array of floats.Template:R

Integer numbersEdit

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} An integer data type represents some range of mathematical integers. Integers may be either signed (allowing negative values) or unsigned (non-negative integers only). Common ranges are:

Size (bytes) Size (bits) Names Signed range (two's complement representation) Unsigned range
1 byte 8 bits Byte, octet, minimum size of char in C99( see limits.h CHAR_BIT) −128 to +127 0 to 255
2 bytes 16 bits x86 word, minimum size of short and int in C −32,768 to +32,767 0 to 65,535
4 bytes 32 bits citation CitationClass=web

}}</ref> pointer for IA-32-compatible processors

−2,147,483,648 to +2,147,483,647 0 to 4,294,967,295
8 bytes 64 bits x86 quadruple word, minimum size of long long in C, actual size of long for most modern C compilers,<ref name=agnerfog /> pointer for x86-64-compatible processors −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 0 to 18,446,744,073,709,551,615

Floating-point numbersEdit

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} A floating-point number represents a limited-precision rational number that may have a fractional part. These numbers are stored internally in a format equivalent to scientific notation, typically in binary but sometimes in decimal. Because floating-point numbers have limited precision, only a subset of real or rational numbers are exactly representable; other numbers can be represented only approximately. Many languages have both a single-precision (often called float) and a double-precision type (often called double).

BooleansEdit

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} A Boolean type, typically denoted bool or boolean, is typically a logical type that can have either the value true or the value false. Although only one bit is necessary to accommodate the value set true and false, programming languages typically implement Boolean types as one or more bytes.

Many languages (e.g. Java, Pascal and Ada) implement Booleans adhering to the concept of Boolean as a distinct logical type. Some languages, though, may implicitly convert Booleans to numeric types at times to give extended semantics to Booleans and Boolean expressions or to achieve backwards compatibility with earlier versions of the language. For example, early versions of the C programming language that followed ANSI C and its former standards did not have a dedicated Boolean type. Instead, numeric values of zero are interpreted as false, and any other value is interpreted as true.<ref>Template:Cite book</ref> The newer C99 added a distinct Boolean type _Bool (the more intuitive name bool as well as the macros true and false can be included with stdbool.h),<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and C++ supports bool as a built-in type and true and false as reserved words.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Specific languagesEdit

JavaEdit

The Java virtual machine's set of primitive data types consists of:<ref>Template:Cite book</ref>

  • byte, short, int, long, char (integer types with a variety of ranges)
  • float and double, floating-point numbers with single and double precisions
  • boolean, a Boolean type with logical values true and false
  • returnAddress, a value referring to an executable memory address. This is not accessible from the Java programming language and is usually left out.<ref>Template:Cite book</ref><ref>Template:Cite book</ref>

C basic typesEdit

{{#invoke:Labelled list hatnote|labelledList|Main article|Main articles|Main page|Main pages}} The set of basic C data types is similar to Java's. Minimally, there are four types, char, int, float, and double, but the qualifiers short, long, signed, and unsigned mean that C contains numerous target-dependent integer and floating-point primitive types.<ref>Template:Cite book</ref> C99 extended this set by adding the Boolean type _Bool and allowing the modifier long to be used twice in combination with int (e.g. long long int).<ref name=c99newtypes>Template:Cite book</ref>

XML SchemaEdit

The XML Schema Definition language provides a set of 19 primitive data types:<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

JavaScriptEdit

In JavaScript, there are 7 primitive data types: string, number, bigint, boolean, symbol, undefined, and null.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Their values are considered immutable. These are not objects and have no methods or properties; however, all primitives except undefined and null have object wrappers.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

Visual Basic .NETEdit

In Visual Basic .NET, the primitive data types consist of 4 integral types, 2 floating-point types, a 16-byte decimal type, a Boolean type, a date/time type, a Unicode character type, and a Unicode string type.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

RustEdit

Rust has primitive unsigned and signed fixed width integers in the format u or i respectively followed by any bit width that is a power of two between 8 and 128 giving the types u8, u16, u32, u64, u128, i8, i16, i32, i64 and i128.<ref name=":0">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Also available are the types usize and isize which are unsigned and signed integers that are the same bit width as a reference with the usize type being used for indices into arrays and indexable collection types.<ref name=":0" />

Rust also has:

  • bool for the Boolean type.<ref name=":0" />
  • f32 and f64 for 32 and 64-bit floating point numbers.<ref name=":0" />
  • char for a unicode character. Under the hood these are unsigned 32-bit integers with values that correspond to the char's codepoint but only values that correspond to a valid unicode scalar value are valid.<ref name=":0" />

Built-in typesEdit

Built-in types are distinguished from others by having specific support in the compiler or runtime, to the extent that it would not be possible to simply define them in a header file or standard library module.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Besides integers, floating-point numbers, and Booleans, other built-in types include:

Characters and stringsEdit

A character type is a type that can represent all Unicode characters, hence must be at least 21 bits wide. Some languages such as Julia include a true 32-bit Unicode character type as primitive.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Other languages such as JavaScript, Python, Ruby, and many dialects of BASIC do not have a primitive character type but instead add strings as a primitive data type, typically using the UTF-8 encoding. Strings with a length of one are normally used to represent single characters.

Some languages have character types that are too small to represent all Unicode characters. These are more properly categorized as integer types that have been given a misleading name. For example C includes a char type, but it is defined to be the smallest addressable unit of memory, which several standards (such as POSIX) require to be 8 bits. Recent versions of these standards refer to char as a numeric type. char is also used for a 16-bit integer type in Java, but again this is not a Unicode character type.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

The term string also does not always refer to a sequence of Unicode characters, instead referring to a sequence of bytes. For example, x86-64 has string instructions to move, set, search, or compare a sequence of items, where an item could be 1, 2, 4, or 8 bytes long.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

See alsoEdit

ReferencesEdit

Template:Reflist

External linksEdit

Template:Data types