值類型與引用類型

維基百科,自由的百科全書

計算機編程中的數據類型可分為兩族:值類型(value type)與引用類型(reference type)。值類型的值就是實際值。引用類型的值是到其他值的引用[1][2]

各種程式語言的分類[編輯]

語言 值類型 引用類型
C++[3] 布爾值、字符、整數、浮點數、類(包括字符串、列表、映射、集合、堆疊、隊列)、枚舉 參考,指針
Java[4] 布爾值、字符、整數、浮點數 數組、類(包括不可變字符串、列表、字典、集合、堆疊、隊列、枚舉)、接口、空指針
C#[5] structures (including booleans, characters, integer numbers, floating-point numbers, fixed-point numbers, lists, dictionaries, sets, stacks, queues, optionals), enumerations classes (including immutable strings, arrays, tuples, lists, dictionaries, sets, stacks, queues), interfaces, pointers
Swift[6][7] structures (including booleans, characters, integer numbers, floating-point numbers, fixed-point numbers, mutable strings, tuples, mutable arrays, mutable dictionaries, mutable sets), enumerations (including optionals), and user-defined structures and enumerations composing other value types. functions, closures, classes
Python[8] classes (including immutable booleans, immutable integer numbers, immutable floating-point numbers, immutable complex numbers, immutable strings, byte strings, immutable byte strings, immutable tuples, immutable ranges, immutable memory views, lists, dictionaries, sets, immutable sets, null pointer)
JavaScript[9] immutable booleans, immutable floating-point numbers, immutable integer numbers (bigint), immutable strings, immutable symbols, undefined, null objects (including functions, arrays, typed arrays, sets, maps, weak sets and weak maps)
OCaml[10][11] immutable characters, immutable integer numbers, immutable floating-point numbers, immutable tuples, immutable enumerations (including immutable units, immutable booleans, immutable lists, immutable optionals), immutable exceptions, immutable formatting strings arrays, immutable strings, byte strings, dictionaries (including pointers)

裝箱和拆箱(boxing and unboxing)[編輯]

區分值類型與引用類型的語言,典型包括了裝箱機制。即把一些甚至全部值類型包為引用類型。這使得值類型可用在需要引用類型的場合。逆過程稱為拆箱。

參見[編輯]

參考文獻[編輯]

  1. ^ Brown, Erik E. Windows Forms in Action. Shelter Island, New York: Manning. 2006: 703. ISBN 978-1-932-39465-8. 
  2. ^ Stephens, Rod. C# 5.0 Programmer's Reference. Indianapolis, Indiana: John Wiley & Sons. 2014: 57. ISBN 978-1-118-84728-2. 
  3. ^ Type - cppreference.com. en.cppreference.com. [2021-01-08]. (原始內容存檔於2020-11-13). 
  4. ^ Chapter 4. Types, Values, and Variables. docs.oracle.com. [2021-01-08]. (原始內容存檔於2020-08-29). 
  5. ^ C# Keywords. docs.microsoft.com. [2021-01-08]. (原始內容存檔於2020-11-26). 
  6. ^ Structures and Classes — The Swift Programming Language (Swift 5.2). docs.swift.org. [2021-01-08]. (原始內容存檔於2020-12-11). 
  7. ^ Closures — The Swift Programming Language (Swift 5.2). docs.swift.org. [2021-01-08]. (原始內容存檔於2021-02-06). 
  8. ^ Built-in Types — Python 3.8.2rc1 documentation. docs.python.org. [2021-01-08]. (原始內容存檔於2020-06-14). 
  9. ^ ECMAScript® 2019 Language Specification. www.ecma-international.org. [2021-01-08]. (原始內容存檔於2021-01-13). 
  10. ^ Chapter 24 The core library. caml.inria.fr. [2021-01-08]. (原始內容存檔於2020-07-02). 
  11. ^ Modifiable Data Structures. caml.inria.fr. [2021-01-08]. (原始內容存檔於2020-07-22).