Primitive data type

Last updated

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. [3] [4] Data types which are not primitive are referred to as derived or composite. [3]

Contents

Primitive types are almost always value types, but composite types may also be value types. [5]

Common primitive data types

The Java virtual machine's set of primitive data types is: [6]

These primitive types are in general precisely those supported by computer hardware, except possibly for varying integer sizes or hardware that is missing floating point. Operations on such primitives 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. [9] 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. [10] 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. [9] :113

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. [11]

Integer numbers

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 byte8 bits Byte, octet, minimum size of char in C99( see limits.h CHAR_BIT)−128 to +1270 to 255
2 bytes16 bits x86 word, minimum size of short and int in C−32,768 to +32,7670 to 65,535
4 bytes32 bitsx86 double word, minimum size of long in C, actual size of int for most modern C compilers, [12] pointer for IA-32-compatible processors−2,147,483,648 to +2,147,483,6470 to 4,294,967,295
8 bytes64 bitsx86 quadruple word, minimum size of long long in C, actual size of long for most modern C compilers, [12] pointer for x86-64-compatible processors−9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615

Floating-point numbers

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").

Booleans

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". [13] The newer C99 added a distinct boolean type that can be included with stdbool.h, [14] and C++ supports bool as a built-in type and "true" and "false" as reserved words. [15]

XML Schema

The XML Schema Definition language provides a set of 19 primitive data types: [16]

Built-in types

In JavaScript, there are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null. These are not objects and have no methods. [18]

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. [19]

In general, data types that can be built into sophisticated programming languages include:

Characters and strings

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. [20] 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" or "string" types that are too small to represent all Unicode characters or strings. These are more properly categorized as integer types. 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. The recent version 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. [21] Some computer hardware has a few instructions which deal with "strings", referring to a sequence of bytes. For example, x86-64 can move, set, search, or compare a sequence of items, where an item could be 1, 2, 4, or 8 bytes long. [22]

Ranges

A range numeric data type has its maximum and minimum value embedded in the type. It is included in some languages such as Ada and Pascal. 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. In practice the compiler chooses the most appropriate primitive integer or floating-point type automatically.

See also

Related Research Articles

In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set of integer sizes available varies between different types of computers. Computer hardware nearly always provides a way to represent a processor register or memory address as an integer.

Double-precision floating-point format is a computer number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.

S-expression

In computer programming, an S-expression is an expression in a like-named notation for nested list (tree-structured) data. S-expressions were invented for and popularized by the programming language Lisp, which uses them for source code as well as data.

Data type Attribute of data

In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support basic data types of integer numbers, floating-point numbers, characters and Booleans. A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. A data type provides a set of values from which an expression may take its values.

In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa. Type conversions can take advantage of certain features of type hierarchies or data representations. Two important aspects of a type conversion are whether it happens implicitly (automatically) or explicitly, and whether the underlying data representation is converted from one representation into another, or a given representation is merely reinterpreted as the representation of another data type. In general, both primitive and compound data types can be converted.

In computer science, the Boolean is a data type that has one of two possible values which is intended to represent the two truth values of logic and Boolean algebra. It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century. The Boolean data type is primarily associated with conditional statements, which allow different actions by changing control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. It is a special case of a more general logical data type —logic doesn't always need to be Boolean.

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.

JSON Open standard file format and data interchange

JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays. It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.

Action Message Format (AMF) is a binary format used to serialize object graphs such as ActionScript objects and XML, or send messages between an Adobe Flash client and a remote service, usually a Flash Media Server or third party alternatives. The Actionscript 3 language provides classes for encoding and decoding from the AMF format.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

Some programming languages provide a complex data type for complex number storage and arithmetic as a built-in (primitive) data type.

BSON is a computer data interchange format. The name "BSON" is based on the term JSON and stands for "Binary JSON". It is a binary form for representing simple or complex data structures including associative arrays, integer indexed arrays, and a suite of fundamental scalar types. BSON originated in 2009 at MongoDB. Several scalar data types are of specific interest to MongoDB and the format is used both as a data storage and network transfer format for the MongoDB database, but it can be used independently outside of MongoDB. Implementations are available in a variety of languages such as C, C++, C#, D, Delphi, Erlang, Go, Haskell, Java, JavaScript, Julia, Lua, OCaml, Perl, PHP, Python, Ruby, Rust, Scala, Smalltalk, and Swift.

This is a comparison of data-serialization formats, various ways to convert complex objects to sequences of bits. It does not include markup languages used exclusively as document file formats.

MessagePack is a computer data interchange format. It is a binary form for representing simple data structures like arrays and associative arrays. MessagePack aims to be as compact and simple as possible. The official implementation is available in a variety of languages such as C, C++, C#, D, Erlang, Go, Haskell, Java, JavaScript (NodeJS), Lua, OCaml, Perl, PHP, Python, Ruby, Scala, Smalltalk, and Swift.

Some programming languages provide a built-in (primitive) or library decimal data type to represent non-repeating decimal fractions like 0.3 and -1.17 without rounding, and to do arithmetic on them. Examples are the decimal.Decimal type of Python, and analogous types provided by other languages.

Universal Binary JSON (UBJSON) is a computer data interchange format. It is a binary form directly imitating JSON, but requiring fewer bytes of data. It aims to achieve the generality of JSON, combined with being much easier to process than JSON.

A character literal is a type of literal in programming for the representation of a single character's value within the source code of a computer program.

JSON→URL Text-based data interchange format designed for use in a URL query string

JSON→URL is a language-independent data interchange format for the JSON data model suitable for use within a URL/URI query string. It is defined by an open specification, though not through a standards body.

References

  1. Stone, R. G.; Cooke, D. J. (5 February 1987). Program Construction. Cambridge University Press. p. 18. ISBN   978-0-521-31883-9.
  2. Wikander, Jan; Svensson, Bertil (31 May 1998). Real-Time Systems in Mechatronic Applications. Springer Science & Business Media. p. 101. ISBN   978-0-7923-8159-4.
  3. 1 2 Khurana, Rohit. Data and File Structure (For GTU), 2nd Edition. Vikas Publishing House. p. 2. ISBN   978-93-259-6005-3.
  4. Chun, Wesley (2001). Core Python Programming. Prentice Hall Professional. p. 77. ISBN   978-0-13-026036-9.
  5. Olsen, Geir; Allison, Damon; Speer, James (1 January 2008). Visual Basic .NET Class Design Handbook: Coding Effective Classes. Apress. p. 80. ISBN   978-1-4302-0780-1.
  6. 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.
  7. Cowell, John (18 February 1997). Essential Java Fast: How to write object oriented software for the Internet. Springer Science & Business Media. p. 27. ISBN   978-3-540-76052-8.
  8. Rakshit, Sandip; Panigrahi, Goutam (December 1995). A Hand Book of Objected Oriented Programming With Java. S. Chand Publishing. p. 11. ISBN   978-81-219-3001-7.
  9. 1 2 Fog, Agner. "Optimizing software in C++" (PDF). p. 29. Retrieved 28 January 2022. Integer operations are fast in most cases, [...]
  10. "Single Instruction Single Data - an overview | ScienceDirect Topics".
  11. Kernighan, Brian W.; Ritchie, Dennis M. (1988). "2.2 Data Types and Sizes". The C programming language (Second ed.). Englewood Cliffs, N.J. p. 36. ISBN   0131103709.
  12. 1 2 Fog, Agner (2010-02-16). "Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation" (PDF). Retrieved 2010-08-30.
  13. Kernighan, Brian W; Ritchie, Dennis M (1978). The C Programming Language (1st ed.). Englewood Cliffs, NJ: Prentice Hall. p.  41. ISBN   0-13-110163-3.
  14. "Boolean type support library". devdocs.io. Retrieved October 15, 2020.
  15. "Bool data type in C++". GeeksforGeeks. 5 June 2017. Retrieved October 15, 2020.
  16. Biron, Paul V.; Malhotra, Ashok. "XML Schema Part 2: Datatypes". www.w3.org (Second ed.). Retrieved 29 January 2022.
  17. Phillips, Lee Anne (18 January 2002). "Declaring a NOTATION | Understanding XML Document Type Definitions". www.informit.com. Retrieved 29 January 2022.
  18. "Primitive - MDN Web Docs Glossary: Definitions of Web-related terms". developer.mozilla.org. MDN.
  19. "Types in Visual Basic". Microsoft Docs. 18 September 2021. Retrieved 18 May 2022.
  20. "Strings · The Julia Language". docs.julialang.org. Retrieved 29 January 2022.
  21. Mansoor, Umer (8 May 2016). "The char Type in Java is Broken". CodeAhoy. Retrieved 10 February 2020.
  22. "I/O and string instructions" . Retrieved 29 January 2022.