Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Primitive data type
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Extremely basic data type}} In [[computer science]], '''primitive data types''' are a set of basic [[data type]]s from which all other data types are constructed.<ref>{{cite book |last1=Stone |first1=R. G. |url=https://books.google.com/books?id=0k_xz8O2SewC&pg=PA18 |title=Program Construction |last2=Cooke |first2=D. J. |date=5 February 1987 |publisher=Cambridge University Press |isbn=978-0-521-31883-9 |page=18 |language=en-US}}</ref> Specifically it often refers to the limited set of data representations in use by a particular [[central processing unit|processor]], which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary.<ref>{{cite book |last1=Wikander |first1=Jan |last2=Svensson |first2=Bertil |title=Real-Time Systems in Mechatronic Applications |date=31 May 1998 |publisher=Springer Science & Business Media |isbn=978-0-7923-8159-4 |page=101 |url=https://books.google.com/books?id=fDCNR7VwG-AC&pg=PA101 |language=en}}</ref> More generally, ''primitive data types'' may refer to the standard data types built into a [[programming language]] ('''built-in types''').<ref name=Khurana>{{cite book |last1=Khurana |first1=Rohit |title=Data and File Structure (For GTU), 2nd Edition |publisher=Vikas Publishing House |isbn=978-93-259-6005-3 |page=2 |url=https://books.google.com/books?id=s0JDDAAAQBAJ&pg=PA2 |language=en}}</ref><ref>{{cite book |last1=Chun |first1=Wesley |title=Core Python Programming |date=2001 |publisher=Prentice Hall Professional |isbn=978-0-13-026036-9 |page=77 |url=https://books.google.com/books?id=mh0bU6NXrBgC&pg=PA77 |language=en}}</ref> Data types which are not primitive are referred to as ''derived'' or [[composite data type|''composite'']].<ref name=Khurana/> Primitive types are almost always [[value type]]s, but composite types may also be value types.<ref>{{cite book |last1=Olsen |first1=Geir |last2=Allison |first2=Damon |last3=Speer |first3=James |title=Visual Basic .NET Class Design Handbook: Coding Effective Classes |date=1 January 2008 |publisher=Apress |isbn=978-1-4302-0780-1 |page=80 |url=https://books.google.com/books?id=DUQnCgAAQBAJ&pg=PA80 |language=en}}</ref> ==Common primitive data types== The most common primitive types are those used and supported by computer hardware, such as [[integer (computer science)|integer]]s of various sizes, [[Floating point|floating-point]] numbers, and [[Boolean data type|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">{{cite web |last1=Fog |first1=Agner |title=Optimizing software in C++ |url=https://www.agner.org/optimize/optimizing_cpp.pdf#page=29 |access-date=28 January 2022 |page=29 |quote=Integer operations are fast in most cases, [...]}}</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, single data|single instruction]].{{fact|date=March 2025}} 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.{{r|Agner|p=113}} ===Integer numbers=== {{Main|Integer (computer science)}} An [[integer (computer science)|integer]] data type represents some [[range (computer programming)|range]] of mathematical integers. Integers may be either signed (allowing negative values) or unsigned ([[non-negative integer]]s only). Common ranges are: {| class="wikitable" |- ! Size ([[byte]]s) ! Size ([[bit]]s) ! Names ! [[Signed number representations|Signed]] range ([[two's complement]] representation) ! Unsigned range |- | 1 byte | 8 bits | [[Byte]], [[octet (computing)|octet]], minimum size of <code>char</code> in [[C (programming language)|C99]]( see [[limits.h]] CHAR_BIT) | −128 to +127 | 0 to 255 |- | 2 bytes | 16 bits | [[x86]] [[Word (computer architecture)|word]], minimum size of <code>short</code> and <code>int</code> in C | −32,768 to +32,767 | 0 to 65,535 |- | 4 bytes | 32 bits | x86 double word, minimum size of <code>long</code> in C, actual size of <code>int</code> for most modern C compilers,<ref name=agnerfog>{{cite web|url=http://www.agner.org/optimize/calling_conventions.pdf|title=Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation |date=2010-02-16 |access-date=2010-08-30 |last=Fog |first=Agner}}</ref> [[Pointer (computer programming)|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 <code>long long</code> in C, actual size of <code>long</code> 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 numbers=== {{Main|Floating-point arithmetic}} 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 numeral system|binary]] but sometimes in [[decimal]]. Because floating-point numbers have limited precision, only a subset of [[real number|real]] or [[rational number|rational]] numbers are exactly representable; other numbers can be represented only approximately. Many languages have both a [[Single-precision floating-point format|single-precision]] (often called ''float'') and a [[Double-precision floating-point format|double-precision]] type (often called ''double''). ===Booleans=== {{Main|Boolean data type}} 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 (programming language)|Java]], [[Pascal (programming language)|Pascal]] and [[Ada (programming language)|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>{{cite book |first1= Brian W |last1= Kernighan |author-link1= Brian Kernighan |first2= Dennis M |last2= Ritchie |author-link2= Dennis Ritchie |page= [https://archive.org/details/cprogramminglang00kern/page/41 41] |title= [[The C Programming Language]] |edition= 1st |publisher= [[Prentice Hall]] |year= 1978 |location= [[Englewood Cliffs, NJ]] |isbn= 0-13-110163-3}}</ref> The newer [[C99]] added a distinct Boolean type <code>_Bool</code> (the more intuitive name <code>bool</code> as well as the macros <code>true</code> and <code>false</code> can be included with [[stdbool.h]]),<ref>{{cite web|url=https://devdocs.io/c/types/boolean|access-date=October 15, 2020|title=Boolean type support library|website=devdocs.io}}</ref> and [[C++]] supports <code>bool</code> as a built-in type and ''true'' and ''false'' as reserved words.<ref>{{cite web|url=https://www.geeksforgeeks.org/bool-data-type-in-c/|access-date=October 15, 2020|title=Bool data type in C++|website=GeeksforGeeks|date=5 June 2017}}</ref> ==Specific languages== ===Java=== The [[Java (programming language)|Java]] virtual machine's set of primitive data types consists of:<ref>{{cite book |last1=Lindholm |first1=Tim |last2=Yellin |first2=Frank |last3=Bracha |first3=Gilad |last4=Buckley |first4=Alex |title=The Java® Virtual Machine Specification |date=13 February 2015 |url=https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.3 |chapter=Chapter 2. The Structure of the Java Virtual Machine}}</ref> * <code>byte</code>, <code>short</code>, <code>int</code>, <code>long</code>, <code>char</code> ([[integer (computer science)|integer]] types with a variety of ranges) * <code>float</code> and <code>double</code>, [[Floating point|floating-point number]]s with single and double [[precision (computer science)|precisions]] * <code>boolean</code>, a [[Boolean data type|Boolean]] type with logical values <code>true</code> and <code>false</code> * <code>returnAddress</code>, a value referring to an executable memory address. This is not accessible from the Java programming language and is usually left out.<ref>{{cite book |last1=Cowell |first1=John |title=Essential Java Fast: How to write object oriented software for the Internet |date=18 February 1997 |publisher=Springer Science & Business Media |isbn=978-3-540-76052-8 |page=27 |url=https://books.google.com/books?id=5M9_fBX4QicC&pg=PA27 |language=en}}</ref><ref>{{cite book |last1=Rakshit |first1=Sandip |last2=Panigrahi |first2=Goutam |title=A Hand Book of Objected Oriented Programming With Java |date=December 1995 |publisher=S. Chand Publishing |isbn=978-81-219-3001-7 |page=11 |url=https://books.google.com/books?id=aAsbEAAAQBAJ&pg=PA11 |language=en}}</ref> === C basic types === {{Main|C data types#Basic types}} The set of basic [[C_data_types#Basic_types|C data types]] is similar to Java's. Minimally, there are four types, <code>char</code>, <code>int</code>, <code>float</code>, and <code>double</code>, but the qualifiers <code>short</code>, <code>long</code>, <code>signed</code>, and <code>unsigned</code> mean that C contains numerous target-dependent integer and floating-point primitive types.<ref>{{cite book |last1=Kernighan |first1=Brian W.|last2=Ritchie|first2=Dennis M. |title=The C programming language|chapter=2.2 Data Types and Sizes |date=1988 |location=Englewood Cliffs, N.J. |isbn=0131103709 |page=36 |edition=Second}}</ref> [[C99]] extended this set by adding the Boolean type <code>_Bool</code> and allowing the modifier <code>long</code> to be used twice in combination with <code>int</code> (e.g. <code>long long int</code>).<ref name=c99newtypes>{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf | title=ISO/IEC 9899:1999 specification, TC3 | at=p. 255, § 6.2.5 ''Types''}}</ref> === XML Schema === The [[XML Schema Definition]] language provides a set of 19 primitive data types:<ref>{{cite web |last1=Biron |first1=Paul V. |last2=Malhotra |first2=Ashok |title=XML Schema Part 2: Datatypes |url=https://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes |website=www.w3.org |access-date=29 January 2022 |edition=Second}}</ref> * <code>string</code>: a [[String (computer science)|string]], a sequence of [[Unicode code point]]s * <code>boolean</code>: a [[Boolean data type|Boolean]] * <code>decimal</code>: a number represented with [[decimal]] notation * <code>float</code> and <code>double</code>: [[floating-point]] numbers * <code>duration</code>, <code>dateTime</code>, <code>time</code>, <code>date</code>, <code>gYearMonth</code>, <code>gYear</code>, <code>gMonthDay</code>, <code>gDay</code>, and <code>gMonth</code>: [[Calendar date]]s and times * <code>hexBinary</code> and <code>base64Binary</code>: [[binary data]] encoded as [[hexadecimal]] or [[Base64]] * <code>anyURI</code>: a [[URI]] * <code>QName</code>: a [[QName|qualified name]] * <code>NOTATION</code>: a QName declared as a notation in the schema. Notations are used to embed non-XML data types.<ref>{{cite news |last1=Phillips |first1=Lee Anne |title=Declaring a NOTATION {{!}} Understanding XML Document Type Definitions |url=https://www.informit.com/articles/article.aspx?p=24992&seqNum=5 |access-date=29 January 2022 |work=www.informit.com |date=18 January 2002}}</ref> This type cannot be used directly - only derived types that enumerate a limited set of QNames may be used. === JavaScript === In JavaScript, there are 7 primitive data types: string, number, bigint, boolean, symbol, undefined, and null.<ref>{{cite web |title=Primitive - MDN Web Docs Glossary: Definitions of Web-related terms |url=https://developer.mozilla.org/en-US/docs/Glossary/Primitive |date=8 June 2023 |publisher=MDN}}</ref> Their values are considered [[immutable]]. These are not objects and have no [[method (computer programming) | methods]] or [[property (programming) | properties]]; however, all primitives except undefined and null have [[object wrapper | object wrappers]].<ref>{{cite web |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values |title=JavaScript data types and data structures |publisher=MDN |date=9 July 2024}}</ref> === Visual Basic .NET === 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>{{cite web |title=Types in Visual Basic |url=https://docs.microsoft.com/en-us/dotnet/visual-basic/reference/language-specification/types#primitive-types |website=Microsoft Docs |access-date=18 May 2022 |language=en-us |date=18 September 2021}}</ref> === Rust === Rust has primitive unsigned and signed fixed width integers in the format <code>u</code> or <code>i</code> respectively followed by any bit width that is a power of two between <code>8</code> and <code>128</code> giving the types <code>u8</code>, <code>u16</code>, <code>u32</code>, <code>u64</code>, <code>u128</code>, <code>i8</code>, <code>i16</code>, <code>i32</code>, <code>i64</code> and <code>i128</code>.<ref name=":0">{{Cite web |title=Data Types - The Rust Programming Language |url=https://doc.rust-lang.org/book/ch03-02-data-types.html |access-date=2023-10-17 |website=doc.rust-lang.org}}</ref> Also available are the types <code>usize</code> and <code>isize</code> which are unsigned and signed integers that are the same bit width as a reference with the <code>usize</code> type being used for indices into arrays and indexable collection types.<ref name=":0" /> Rust also has: * <code>bool</code> for the [[Boolean data type|Boolean]] type.<ref name=":0" /> * <code>f32</code> and <code>f64</code> for 32 and 64-bit [[Floating-point arithmetic|floating point numbers]].<ref name=":0" /> * <code>char</code> for a [[Universal Character Set characters|unicode character]]. Under the hood these are unsigned 32-bit integers with values that correspond to the <code>char</code>'s codepoint but only values that correspond to a valid unicode scalar value are valid.<ref name=":0" /> == Built-in types == 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>{{cite web |title=Built-in types (C++) |url=https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170 |website=learn.microsoft.com |language=en-us |date=17 August 2021}}</ref> Besides integers, floating-point numbers, and Booleans, other built-in types include: * The [[void type]] and null pointer type <code>[[nullptr_t]]</code> in [[C++11]] and [[C23 (C standard revision)|C23]] * Characters and strings (see [[#Characters and strings|below]]) * [[Tuple]] in [[Standard ML]], [[Python (programming language)|Python]], [[Scala (programming language)|Scala]], [[Swift (programming language)|Swift]], [[Elixir (programming language)|Elixir]] * [[List (abstract data type)|List]] in [[Common Lisp]], [[Python (programming language)|Python]], [[Scheme (programming language)|Scheme]], [[Haskell (programming language)|Haskell]] * [[fixed-point arithmetic|Fixed-point number]] with a variety of [[precision (computer science)|precisions]] and a programmer-selected [[order of magnitude|scale]]. * [[Complex data type|Complex number]] in [[C99]], [[Fortran]], [[Common Lisp]], [[Python (programming language)|Python]], [[D (programming language)|D]], [[Go (programming language)|Go]]. This is two floating-point numbers, a real part and an imaginary part. * [[Enumerated type]] sequence of names for allowed values; can improve readability and type checking. * [[Rational data type|Rational number]] in [[Common Lisp]] * [[Arbitrary-precision arithmetic|Arbitrary-precision]] <code>Integer</code> type in [[Common Lisp]], [[Erlang (programming language)|Erlang]], [[Haskell (programming language)|Haskell]] * [[Associative array]]s, [[Record (computer science)|record]]s or [[Set (abstract data type)|set]]s in [[Perl]], [[PHP]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[JavaScript]], [[Lua (programming language)|Lua]], [[D (programming language)|D]], [[Go (programming language)|Go]] * [[reference (computer science)|Reference]] (also called a ''[[pointer (computer programming)|pointer]]'' or ''handle'' or ''descriptor''), * [[Symbol (programming)|Symbols]], in [[Lisp (programming language)|Lisp]] * [[First-class function]], in all [[functional programming|functional]] languages, [[JavaScript]], [[Lua (programming language)|Lua]], [[D (programming language)|D]], [[Go (programming language)|Go]], and in newer standards of [[C++]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]], [[Perl]] ===Characters and strings=== A [[character (computing)|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>{{cite web |title=Strings · The Julia Language |url=https://docs.julialang.org/en/v1/manual/strings/#man-characters |website=docs.julialang.org |access-date=29 January 2022}}</ref> Other languages such as [[JavaScript]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], and many dialects of [[BASIC]] do not have a primitive character type but instead add [[String (computer science)|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 <code>char</code> type, but it is defined to be the smallest addressable unit of memory, which several standards (such as [[POSIX]]) require to be 8 [[bit]]s. Recent versions of these standards refer to <code>char</code> as a numeric type. <code>char</code> is also used for a 16-bit integer type in [[Java (programming language)|Java]], but again this is not a Unicode character type.<ref>{{cite web |last1=Mansoor |first1=Umer |title=The char Type in Java is Broken |url=https://codeahoy.com/2016/05/08/the-char-type-in-java-is-broken/ |website=CodeAhoy |date=8 May 2016 |access-date=10 February 2020 |ref=3}}</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>{{cite web|title=I/O and string instructions|access-date=29 January 2022|url=http://linasm.sourceforge.net/docs/instructions/cpu.php#bit}}</ref> ==See also== * {{annotated link|Language primitive}} * {{section link|List of data structures|Data types}} * {{annotated link|Object type}} * {{annotated link|Primitive wrapper class}} * {{annotated link|Variable (computer science)}} ==References== {{reflist}} ==External links== *{{Commons category-inline|Primitive types}} {{data types}} {{DEFAULTSORT:Primitive data type}} [[Category:Data types]] [[Category:Primitive types| ]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Annotated link
(
edit
)
Template:Cite book
(
edit
)
Template:Cite news
(
edit
)
Template:Cite web
(
edit
)
Template:Commons category-inline
(
edit
)
Template:Data types
(
edit
)
Template:Fact
(
edit
)
Template:Main
(
edit
)
Template:R
(
edit
)
Template:Reflist
(
edit
)
Template:Section link
(
edit
)
Template:Short description
(
edit
)