# 变量（计算机科学）

在计算机科学中，变量是程序存储空间中一个有名称的位置，其中保存着一个值，该值可以被读取，并且在大多数语言中可以在程序运行期间被修改。在命令式（过程式）语言中，变量常被描述为一个有名称的盒子：名称绑定到一块内存区域，该区域中的值在执行过程中可以改变<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup>。变量是程序记忆中间结果的基本机制，而围绕它们的设计选择——比如名称何时绑定到类型或内存——在很大程度上塑造了一门语言的特征<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 核心属性 | 变量具有名称、地址（存储位置）、类型（决定值的范围、存储大小和允许的操作）以及值（相关内存单元的内容）<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup> |
| 左值与右值 | 地址是左值，当变量出现在赋值左侧时需要；内容是右值，在右侧使用<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup> |
| 有类型与无类型变量 | Fortran、Pascal、C 和 Java 要求声明类型，以限制变量能存储的内容；Lisp、JavaScript、Python、PHP 和 Ruby 不将类型附加到变量本身，因此变量可以存储任何值<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup> |
| 存储类别 | 变量可以是静态的（由加载器在整个运行期间绑定）、栈动态的（在函数调用时分配）或堆动态的（在运行时分配和释放）<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup> |
| 隐式类型 | 早期 Fortran 按首字母确定变量类型：以 I、J、K、L、M 或 N 开头的名称为整数，其余为实数，除非另有声明<sup>[3](https://citeseerx.ist.psu.edu/document?doi=aa1577cea270d00020efe312554d55b3cfcca090&repid=rep1&type=pdf)</sup> |
| 默认可变性 | Rust 使变量默认不可变，除非用 `mut` 声明；JavaScript 区分可变的 `let` 和常量 `const`；Python 将赋值视为将名称绑定到对象<sup>[4](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html?highlight=nu)</sup><sup> • </sup><sup>[5](https://tc39.es/ecma262/pr/3872/multipage/ecmascript-language-statements-and-declarations.html)</sup><sup> • </sup><sup>[6](https://docs.python.org/3/faq/programming.html?highlight=unboundlocalerror)</sup> |

## 名称、地址、类型与值

现代语言中的每个变量都结合了若干属性。名称是程序员书写的标识符；不同语言在大小写敏感性、允许的字符以及某些名称是否为保留字（不能用作标识符）方面存在差异<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。在 C 中，名称必须以字母或下划线开头，后续可以是字母、下划线或数字，且名称区分大小写，因此 `MYLIMIT`、`mylimit` 和 `MyLimit` 是三个不同的变量<sup>[7](https://teaching.csse.uwa.edu.au/units/CITS2002/lectures/lecture02/singlepage.html)</sup>。

地址是变量所关联的内存位置。在某些语言中，同一个名称可以在不同时间关联到不同地址，例如两个子程序各自有一个同名的局部变量时；多个名称也可以共享一个地址，这样的变量称为别名<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

类型决定变量可以取值的范围、存储所需的字节数以及为这些值定义的操作<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。一个典型的 C 程序用 4 个字节存储一个整数值，用 8 个字节存储一个浮点值<sup>[7](https://teaching.csse.uwa.edu.au/units/CITS2002/lectures/lecture02/singlepage.html)</sup>。值是变量所关联的内存单元的内容；一个变量一次只保存一个值，不保留过去值的历史<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup><sup> • </sup><sup>[7](https://teaching.csse.uwa.edu.au/units/CITS2002/lectures/lecture02/singlepage.html)</sup>。

## 左值与右值

由于变量同时提供内存地址和值，语言必须规定表达式指的是两者中的哪一个。自 Algol 68 以来，惯例是将变量的地址含义称为左值，将值含义称为右值<sup>[8](https://cs.colby.edu/courses/F18/cs333/CS333-S18-lectures.pdf)</sup>。当变量名出现在赋值左侧时，求值必须产生一个地址以存储新值；当它出现在其他地方时，求值产生所存储的值<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup>。用 C 的术语来说，变量 `x` 是左值，但表达式 `x + 0` 不是，尽管两者计算出的数字相同<sup>[9](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Lvalues.html)</sup>。

## 类型与声明

许多语言将类型作为变量本身的属性。在 Fortran、Pascal、C 和 Java 中，类型必须在使用前声明，变量只能存储该类型的值<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup>。Pascal 在其时代将这一点推到了极致：每个常量、变量或函数都有固定的静态类型，为编译器提供了冗余信息，可用于检查类型一致性并在程序运行前检测错误<sup>[10](https://inf.ethz.ch/department/history/meilensteine-forschung/50years-pascal.html)</sup>。

其他语言将类型附加到值而非变量上。在 Lisp、JavaScript、Python、PHP 和 Ruby 中，变量可以存储任何值<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup>。Fortran 走了一条中间道路，采用隐式声明：名称以 I 到 N 开头的变量默认为整数，其余默认为实数，可以用显式声明覆盖默认值<sup>[3](https://citeseerx.ist.psu.edu/document?doi=aa1577cea270d00020efe312554d55b3cfcca090&repid=rep1&type=pdf)</sup>。

## 存储、绑定与生命周期

将变量绑定到内存单元称为分配，绑定时间是变量与属性之间关联建立的时刻。绑定可以发生在语言设计时、实现时、编译时、加载时、链接时或运行时<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

**静态变量**由加载器绑定到内存单元，并保持绑定直到程序终止。它们高效但不够灵活：只有[静态变量](https://www.edgepedia.org/zh/static-variable)的语言无法支持递归<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

**栈动态变量**通常是函数的局部变量和参数，在函数调用时分配在运行时栈上，在函数返回时释放。这使得递归调用易于实现<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup><sup> • </sup><sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

**堆动态变量**从堆中分配，堆是一种可以请求内存并随后释放以供复用的结构。如果程序员必须自己释放内存，他们常常会忘记，从而产生内存泄漏；一些语言提供垃圾回收，自动释放没有任何引用可达的区域，使用引用计数或标记-清除等方法<sup>[1](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)</sup>。变量的生命周期是它绑定到特定内存位置的时间<sup>[2](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)</sup>。

## 可变性与赋值语义

不同语言在变量首次绑定后其值是否可以改变方面存在差异。在 [Rust](https://www.edgepedia.org/zh/rust-programming-language) 中，变量默认不可变：对不可变变量赋第二个值是编译时错误，必须用 `mut` 显式请求可变性，这也向代码的未来读者传达了意图<sup>[4](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html?highlight=nu)</sup>。JavaScript 的 `let` 和 `const` 声明定义作用域为正在运行的执行上下文的词法环境的变量；没有初始化器的 `let` 绑定在绑定求值时被赋值为 `undefined`<sup>[5](https://tc39.es/ecma262/pr/3872/multipage/ecmascript-language-statements-and-declarations.html)</sup>。

在 Python 中，赋值始终是将名称绑定到值，而不是填充一个固定的盒子。执行 `y = x` 不会复制列表；它创建一个引用同一对象的新名称，因此向 `y` 追加元素会改变 `x` 所看到的内容。相比之下，整数是不可变对象，因此 `x = x + 1` 创建一个新对象并将 `x` 重新绑定到它，而 `y` 仍然引用旧对象<sup>[6](https://docs.python.org/3/faq/programming.html?highlight=unboundlocalerror)</sup>。

Rust 走得更远，通过所有权将变量与内存安全联系起来。当一个拥有堆数据的变量被赋值给另一个变量时，所有权发生移动：先前的所有者不能再被使用，这防止两个变量都释放同一块内存——这种缺陷称为双重释放<sup>[11](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?highlight=garbage)</sup>。

## 历史发展

变量作为存储位置的符号名称，随着第一批编译器的出现而变得实用。Fortran 由 IBM 于 1957 年在[约翰·巴克斯](https://www.edgepedia.org/zh/john-backus)（John Backus）的领导下交付，允许程序员使用变量的符号名称来思考数值计算，而将求值顺序的细节留给编译器<sup>[12](https://assets.cambridge.org/97805217/80988/sample/9780521780988ws.pdf)</sup>。早期 Fortran 没有块结构：变量要么是子程序局部的，要么通过 COMMON 块成为全局的，缺乏嵌套和作用域使得大型程序难以分解，并且容易因名称共享而损坏<sup>[13](https://www.geschichte-der-informatik.de/articles/algol_and_the_algol_family/)</sup>。

Algol 60 于 1960 年 5 月由一个国际委员会制定，引入了至今仍具根本性的元素，包括保留关键字、动态数组、用户定义类型和递归，以及嵌套块结构和词法作用域<sup>[14](https://www.ethistory.ethz.ch/rueckblicke/departemente/dinfk/forschung/weitere_seiten/algol/index_EN/popupfriendly/index_EN.html)</sup>。其声明用于将标识符与类型等属性关联，并提供了 `own` 限定符，在该限定符下，变量的值在其声明所在块的多次激活之间得以保留<sup>[15](https://www.softwarepreservation.org/projects/ALGOL/report/Algol60_revised_report_CACM.pdf)</sup>。

[尼克劳斯·维尔特](https://www.edgepedia.org/zh/niklaus-wirth)（Niklaus Wirth）的 Pascal 于 1970 年发表，简化了 Algol 的方法，并删除了 Algol 中允许局部变量在其过程激活结束后继续存在的概念，认为这是复杂性的来源。Pascal 转而将静态类型普遍化，并添加了数组、记录、文件和指针等结构化类型<sup>[10](https://inf.ethz.ch/department/history/meilensteine-forschung/50years-pascal.html)</sup>。后来的语言重新分配了这些选择：函数式语言尽量减少可变变量，偏向不可变数据；而系统级语言如 Rust（由 Mozilla 于 2012 年至 2015 年开发）保留可变变量，但用编译时所有权规则加以约束<sup>[16](https://john.cs.olemiss.edu/~hcc/csci450/2016fall/notes/Fundamentals/HistorySlides.html)</sup><sup> • </sup><sup>[11](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?highlight=garbage)</sup>。

## References

1. [4. Variables, Programming Languages documentation, Keio University](https://web.sfc.keio.ac.jp/~hattori/prog-theory/en/variables.html)
2. [Fundamental Issues of Variables, Montana State University course notes](https://www.cs.montana.edu/courses/spring2005/355/lectures/Variables.html)
3. [History of Programming Languages, CiteSeerX](https://citeseerx.ist.psu.edu/document?doi=aa1577cea270d00020efe312554d55b3cfcca090&repid=rep1&type=pdf)
4. [Variables and Mutability, The Rust Programming Language](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html?highlight=nu)
5. [ECMAScript Language Specification, Let and Const Declarations, TC39](https://tc39.es/ecma262/pr/3872/multipage/ecmascript-language-statements-and-declarations.html)
6. [Programming FAQ, Python documentation](https://docs.python.org/3/faq/programming.html?highlight=unboundlocalerror)
7. [CITS2002 Systems Programming, Lecture 2, University of Western Australia](https://teaching.csse.uwa.edu.au/units/CITS2002/lectures/lecture02/singlepage.html)
8. [CS333 Programming Languages Lecture Notes, Colby College](https://cs.colby.edu/courses/F18/cs333/CS333-S18-lectures.pdf)
9. [Lvalues, GNU C Language Manual](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Lvalues.html)
10. [50 years of Pascal, ETH Zurich Department of Computer Science](https://inf.ethz.ch/department/history/meilensteine-forschung/50years-pascal.html)
11. [What is Ownership?, The Rust Programming Language](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?highlight=garbage)
12. [Programming Language History sample chapter, Cambridge University Press](https://assets.cambridge.org/97805217/80988/sample/9780521780988ws.pdf)
13. [ALGOL: The Language That Taught Programming How to Teach Itself, Geschichte der Informatik](https://www.geschichte-der-informatik.de/articles/algol_and_the_algol_family/)
14. [ETHistory: Algol, ETH Zurich](https://www.ethistory.ethz.ch/rueckblicke/departemente/dinfk/forschung/weitere_seiten/algol/index_EN/popupfriendly/index_EN.html)
15. [Revised Report on the Algorithmic Language Algol 60, Software Preservation Group](https://www.softwarepreservation.org/projects/ALGOL/report/Algol60_revised_report_CACM.pdf)
16. [History of Programming Languages course notes, University of Mississippi](https://john.cs.olemiss.edu/~hcc/csci450/2016fall/notes/Fundamentals/HistorySlides.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Programming language concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/variable-computer-science

English original: https://www.edgepedia.org/variable-computer-science
