Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Primitive data type

From Wikipedia, the free encyclopedia
Extremely basic data type

Incomputer science,primitive data types are sets of basicdata 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 particularprocessor, 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 aprogramming language (built-in types).[3][4] Data types which are not primitive are referred to asderived orcomposite.[3]

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

Common primitive data types

[edit]

The most common primitive types are those used and supported by computer hardware, such asintegers of various sizes,floating-point numbers, andBoolean 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.[6] Integer addition, for example, can be performed as a single machine instruction, and some offer specific instructions to process sequences of characters with asingle instruction.[7] But the choice of primitive data type may affect performance, for example it is faster usingSIMD operations and data types to operate on an array of floats.[6]: 113 

Integer numbers

[edit]
Main article:Integer (computer science)

Aninteger data type represents somerange of mathematical integers. Integers may be either signed (allowing negative values) or unsigned (non-negative integers only). Common ranges are:

Size (bytes)Size (bits)NamesSigned range (two's complement representation)Unsigned range
1 byte8 bitsByte,octet, minimum size ofchar inC99( seelimits.h CHAR_BIT)−128 to +1270 to 255
2 bytes16 bitsx86word, minimum size ofshort andint in C−32,768 to +32,7670 to 65,535
4 bytes32 bitsx86 double word, minimum size oflong in C, actual size ofint for most modern C compilers,[8]pointer forIA-32-compatible processors−2,147,483,648 to +2,147,483,6470 to 4,294,967,295
8 bytes64 bitsx86 quadruple word, minimum size oflong long in C, actual size oflong for most modern C compilers,[8] pointer forx86-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

[edit]
Main article:Floating-point arithmetic

Afloating-point number represents a limited-precisionrational number that may have a fractional part. These numbers are stored internally in a format equivalent toscientific notation, typically inbinary but sometimes indecimal. Because floating-point numbers have limited precision, only a subset ofreal orrational numbers are exactly representable; other numbers can be represented only approximately. Many languages have both asingle-precision (often calledfloat) and adouble-precision type (often calleddouble).

Booleans

[edit]
Main article:Boolean data type

ABoolean type, typically denotedbool orboolean, is typically alogical type that can have either the valuetrue or the valuefalse. Although only one bit is necessary to accommodate the value settrue andfalse, programming languages typically implement Boolean types as one or more bytes.

Many languages (e.g.Java,Pascal andAda) implement Booleans adhering to the concept of Boolean as a distinct logical type. Some languages, though, may implicitly convert Booleans tonumeric 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 followedANSI C and its former standards did not have a dedicated Boolean type. Instead, numeric values of zero are interpreted asfalse, and any other value is interpreted astrue.[9] The newerC99 added a distinct Boolean type_Bool (later moved tobool as well as the keywordstrue andfalse withC23),[10] andC++ supportsbool as a built-in type andtrue andfalse as reserved words.[11]

Specific languages

[edit]

C and C++ basic types

[edit]
Main article:C data types § Basic types

InC andC++, there are minimally four types,char,int,float, anddouble, but the qualifiersshort,long,signed, andunsigned mean that C contains numerous target-dependent integer and floating-point primitive types.[12]C99 allowed the modifierlong to be used twice in combination withint (e.g.long long int).[13] Similarly,signed andunsigned can be used as modifiers for integral types.C23 introduced thebool type as a core data type, while it was always a core data type of C++. C++ also features astd::byte type (similar to Java).

C and C++ also containsize_t,intptr_t and other integer types, for things like indexing arrays and collections. These are not distinct data types, but often aliases and are system-defined. These are perhaps similar to Rustusize andisize.

Java

[edit]

The basic types of Java are similar to that of C. TheJava virtual machine's set of primitive data types consists of:[14]

  • byte,short,int,long,char (integer types with a variety of ranges)
  • float anddouble,floating-point numbers with single and doubleprecisions
  • boolean, aBoolean 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.[15][16]

XML Schema

[edit]

TheXML Schema Definition language provides a set of 19 primitive data types:[17]

JavaScript

[edit]

In JavaScript, there are 7 primitive data types: string, number, bigint, boolean, symbol, undefined, and null.[19] Their values are consideredimmutable. These are not objects and have no methods or properties; however, all primitives except undefined and null have object wrappers.[20]

Visual Basic .NET

[edit]

InVisual 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.[21]

Rust

[edit]

Rust has primitive unsigned and signed fixed width integers in the formatu ori respectively followed by any bit width that is a power of two between8 and128 giving the typesu8,u16,u32,u64,u128,i8,i16,i32,i64 andi128.[22] Also available are the typesusize andisize which are unsigned and signed integers that are the same bit width as a reference with theusize type being used for indices into arrays and indexable collection types.[22]

Rust also has:

  • bool for theBoolean type.[22]
  • f32 andf64 for 32 and 64-bitfloating point numbers.[22]
  • char for aunicode 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.[22]

Built-in types

[edit]

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.[23] Besides integers, floating-point numbers, and Booleans, other built-in types include:

Characters and strings

[edit]

Acharacter type is a type that can represent allUnicode characters, hence must be at least 21 bits wide. Some languages such as Julia include a true 32-bit Unicode character type as primitive.[24] Other languages such asJavaScript,Python,Ruby, and many dialects ofBASIC do not have a primitive character type but instead addstrings as a primitive data type, typically using theUTF-8 encoding. Strings with a length of one are normally used to represent single characters.

Some languages havecharacter 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 achar type, but it is defined to be the smallest addressable unit of memory, which several standards (such asPOSIX) require to be 8bits. Recent versions of these standards refer tochar as a numeric type.char is also used for a 16-bit integer type inJava, but again this is not a Unicode character type.[25]

The termstring also does not always refer to a sequence of Unicode characters, instead referring to a sequence of bytes. For example, x86-64 hasstring instructions to move, set, search, or compare a sequence of items, where an item could be 1, 2, 4, or 8 bytes long.[26]

See also

[edit]

References

[edit]
  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. ^abKhurana, 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. ^abFog, Agner."Optimizing software in C++"(PDF). p. 29. Retrieved28 January 2022.Integer operations are fast in most cases, [...]
  7. ^Hennessy, John L.; Patterson, David A. (August 2011).Computer Architecture, Fifth Edition: A Quantitative Approach (5th ed.). San Francisco, CA, USA: Morgan Kaufmann Publishers Inc.ISBN 978-0-12-383872-8.
  8. ^abFog, Agner (2010-02-16)."Calling conventions for different C++ compilers and operating systems: Chapter 3, Data Representation"(PDF). Retrieved2010-08-30.
  9. ^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.
  10. ^"Boolean type support library".devdocs.io. RetrievedOctober 15, 2020.
  11. ^"Bool data type in C++".GeeksforGeeks. 5 June 2017. RetrievedOctober 15, 2020.
  12. ^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.{{cite book}}: CS1 maint: location missing publisher (link)
  13. ^ISO/IEC 9899:1999 specification, TC3(PDF). p. 255, § 6.2.5Types.
  14. ^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.
  15. ^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.
  16. ^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.
  17. ^Biron, Paul V.; Malhotra, Ashok."XML Schema Part 2: Datatypes".www.w3.org (Second ed.). Retrieved29 January 2022.
  18. ^Phillips, Lee Anne (18 January 2002)."Declaring a NOTATION | Understanding XML Document Type Definitions".www.informit.com. Retrieved29 January 2022.
  19. ^"Primitive - MDN Web Docs Glossary: Definitions of Web-related terms". MDN. 8 June 2023.
  20. ^"JavaScript data types and data structures". MDN. 9 July 2024.
  21. ^"Types in Visual Basic".Microsoft Docs. 18 September 2021. Retrieved18 May 2022.
  22. ^abcde"Data Types - The Rust Programming Language".doc.rust-lang.org. Retrieved2023-10-17.
  23. ^"Built-in types (C++)".learn.microsoft.com. 17 August 2021.
  24. ^"Strings · The Julia Language".docs.julialang.org. Retrieved29 January 2022.
  25. ^Mansoor, Umer (8 May 2016)."The char Type in Java is Broken".CodeAhoy. Retrieved10 February 2020.
  26. ^"I/O and string instructions". Retrieved29 January 2022.

External links

[edit]
Uninterpreted
Numeric
Reference
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Primitive_data_type&oldid=1338409336"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp