Primitive data type
In computer science, primitive data types are a set of basic data types from which all other data types are constructed.[1] 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.[2] More generally, "primitive data types" may refer to the standard data types built into a programming language (built-in types).[3][4] Data types which are not primitive are referred to as derived or composite.[3]
Primitive types are almost always
Common primitive data types
The most common primitive types are those used and supported by computer hardware, such as
Integer numbers
An
Size (bytes) | Size (bits) | Names | Signed range (two's complement representation) | Unsigned range |
---|---|---|---|---|
1 byte | 8 bits | 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 | x86 double word, minimum size of long in C, actual size of int for most modern C compilers,[8] 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,[8] 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
A
Booleans
A
Many languages (e.g.
bool
as a built-in type and "true" and "false" as reserved words.[11]Specific languages
Java
The Java virtual machine's set of primitive data types consists of:[12]
byte
,short
,int
,long
,char
(integer types with a variety of ranges)float
anddouble
,floating-point numbers with single and double precisionsboolean
, a Boolean type with logical valuestrue
andfalse
returnAddress
, a value referring to an executable memory address. This is not accessible from the Java programming language and is usually left out.[13][14]
C basic types
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.[15] 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
).[16]
XML Schema
The
string
: aUnicode code pointsboolean
: abooleandecimal
: a number represented with decimal notationfloat
anddouble
:floating-pointnumbersduration
,dateTime
,time
,date
,gYearMonth
,gYear
,gMonthDay
,gDay
, andgMonth
: Calendar dates and timeshexBinary
andbase64Binary
: binary data encoded as hexadecimal or Base64anyURI
: aURIQName
: a qualified nameNOTATION
: a QName declared as a notation in the schema. Notations are used to embed non-XML data types.[18] 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, undefined, symbol, and null. These are not objects and have no methods.[19]
Visual Basic .NET
In
Rust
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
.[21] 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.[21]
Rust also has:
bool
for the boolean type.[21]f32
andf64
for 32 and 64-bit floating point numbers.[21]char
for a unicode character. Under the hood these are unsigned 32-bit integers with values that correspond to thechar
's codepoint but only values that correspond to a valid unicode scalar value are valid.[21]
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.[22] Besides integers, floating-point numbers, and Booleans, other built-in types include:
- The nullptr_t in C++ and C23
- Characters and strings (see below)
- Tuple in Standard ML, Python, Scala, Swift, Elixir
- Haskell
- Fixed-point number with a variety of precisions and a programmer-selected scale.
- Complex number in C99, Fortran, Common Lisp, Python, D, Go. This is two floating-point numbers, a real part and an imaginary part.
- Rational number in Common Lisp
- Haskell
- Associative arrays, records, and/or sets in Perl, PHP, Python, Ruby, JavaScript, Lua, D, Go
- Reference (also called a pointer or handle or descriptor),
- Symbols, in Lisp
- First-class function, in all functional languages, JavaScript, Lua, D, Go, and in newer standards of C++, Java, C#, Perl
Characters and strings
A
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.[24]
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.[25]
See also
- Language primitive
- List of data structures § Data types
- Object type
- Primitive wrapper class
- Variable (computer science)
References
- ISBN 978-0-521-31883-9.
- ISBN 978-0-7923-8159-4.
- ^ ISBN 978-93-259-6005-3.
- ISBN 978-0-13-026036-9.
- ISBN 978-1-4302-0780-1.
- ^ a b Fog, Agner. "Optimizing software in C++" (PDF). p. 29. Retrieved 28 January 2022.
Integer operations are fast in most cases, [...]
- ^ "Single Instruction Single Data - an overview | ScienceDirect Topics".
- ^ a b Fog, Agner (2010-02-16). "Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation" (PDF). Retrieved 2010-08-30.
- ISBN 0-13-110163-3.
- ^ "Boolean type support library". devdocs.io. Retrieved October 15, 2020.
- ^ "Bool data type in C++". GeeksforGeeks. 5 June 2017. Retrieved October 15, 2020.
- ^ Lindholm, Tim; Yellin, Frank; Bracha, Gilad; Buckley, Alex (13 February 2015). "Chapter 2. The Structure of the Java Virtual Machine". The Java® Virtual Machine Specification.
- ISBN 978-3-540-76052-8.
- ISBN 978-81-219-3001-7.
- ISBN 0131103709.)
{{cite book}}
: CS1 maint: location missing publisher (link - ^ ISO/IEC 9899:1999 specification, TC3 (PDF). p. 255, § 6.2.5 Types.
- ^ Biron, Paul V.; Malhotra, Ashok. "XML Schema Part 2: Datatypes". www.w3.org (Second ed.). Retrieved 29 January 2022.
- ^ Phillips, Lee Anne (18 January 2002). "Declaring a NOTATION | Understanding XML Document Type Definitions". www.informit.com. Retrieved 29 January 2022.
- ^ "Primitive - MDN Web Docs Glossary: Definitions of Web-related terms". developer.mozilla.org. MDN. 8 June 2023.
- ^ "Types in Visual Basic". Microsoft Docs. 18 September 2021. Retrieved 18 May 2022.
- ^ a b c d e "Data Types - The Rust Programming Language". doc.rust-lang.org. Retrieved 2023-10-17.
- ^ "Built-in types (C++)". learn.microsoft.com. 17 August 2021.
- ^ "Strings · The Julia Language". docs.julialang.org. Retrieved 29 January 2022.
- ^ Mansoor, Umer (8 May 2016). "The char Type in Java is Broken". CodeAhoy. Retrieved 10 February 2020.
- ^ "I/O and string instructions". Retrieved 29 January 2022.
External links
- Media related to Primitive types at Wikimedia Commons