✔ 最佳答案
In computer science and mathematics, a variable (sometimes called a pronumeral) is a symbol denoting a quantity or symbolic representation. In mathematics, a variable often represents an unknown quantity that has the potential to change; in computer science, it represents a place where a quantity can be stored. Variables are often contrasted with constants, which are known and unchanging.
The term has a similar meaning in the physical sciences and engineering: a variable is a quantity whose value may vary over the course of an experiment (including simulations), across samples, during the operation of a system. Variables are generally distinct from parameters, although what is a variable in one context may be a parameter in another. For more on this distinction, see the article on "parameter".
Constants
A constant variable is similar to an immutable variable, but while the value referenced by an immutable variable cannot change during program execution, with a constant it is the location referenced by the constant that cannot change. Since constant variables are often bound to literal values, which are themselves immutable, constant variables are often themselves immutable (e.g. const int HoursPerDay=24). Although a constant value is specified only once, the constant variable can be referenced multiple times in a program. Using a constant instead of specifying a value multiple times in the program can simplify code maintenance, not only to simplify changing its value but also to supply a meaningful name for it and to consolidate such constant assignments to a standard code location, for example at the beginning.
Programming languages provide one of two kinds of constant variables:
Static constant or Manifest constant
Languages such as Visual Basic allow assigning to static constant a fixed value, which will be known before the program starts. Such a constant has the same value each time its program runs. Changing the value is accomplished by changing (and possibly recompiling) the code. E.g.: CONST a = 60
Dynamic constant
Languages such as Java allow assigning to a dynamic constant an expression, possibly involving non-constant operands. The value of such constants may rely on variables defined while a computer program is running. Thus, unlike static constants, the values of dynamic constants cannot be determined at compile time. E.g.: final int a = b + 20;.
Although such dynamic constants have fixed values if they are primitive types (e.g. int), their values may change if they contain a reference to an object, as shown in the following example: