Constant (computer programming)
Constants
A constant is something whose value does not change in the course of the problem under investigation. As noted above, in mathematics, which symbols represent variables and which represent constants is to a large extent arbitrary, and must be specified in each particular problem, though conventions about which letters stand for constants and which letters stand for variables is often assumed. (For example, most beginning students of calculus will say that the derivative of xn is nxn - 1, but this is only true under the assumption that x is a variable and n is a constant.) In a spreadsheet, in one problem it may be useful to hold DATE constant and vary COST; in another problem it may be useful to hold COST constant and vary DATE. In the first case, we might be looking at the cost of a particular item on a particular date from various vendors. In the second case we might be looking for the date on which we could buy an item for a particular price.
Many programming languages, on the other hand, make an explicit syntactic distinction bewteen constant and variable symbols.
In many machine assembly languages or instruction set specifications, constant values are termed immediate values because they are available immediately (often embedded as part of the instruction stream) without needing to load a value from the data cache[1].
Although a constant value is specified only once, the constant can be referenced multiple times in a program. Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings 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 a fixed value to static constant which will be known at compile time. 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 C++ and Java allow initializing a dynamic constant with a value that is computed at runtime. Unlike static constants, the values of dynamic constants cannot be determined until run time. For example:
final int a = b + 20;
.
For variables which are references, do not confuse constant references with immutable objects. For example, when a non-constant reference references an immutable object, that reference can be changed to reference a different object, but the object it originally pointed to cannot be changed (i.e. other references that reference it still see the same information).
Conversely, a constant reference may reference a mutable object. In this case, the reference will always reference the same object (the reference cannot be changed); however, the object which is referenced can still be changed (and other references which also reference that object will see the change), as shown in the following example:
final StringBuffer sampleDynamicConstant = new StringBuffer ("InitialValueOfDynamicConstant");
sampleDynamicConstant.append("_AppendedText");
System.out.println(sampleDynamicConstant);
The above code produces the following output:
InitialValueOfDynamicConstant_AppendedText
In languages where a variable can be an object (i.e. C++), such a variable being constant is equivalent to the immutability of that object.
A popular naming convention for symbolic constants is to give them a name wherein all the letters are capitalized, sometime separated by underscores; for example: SOME_CONSTANT
- ^ IBM Systems Information. Instruction Set - Assembler Language Reference for PowerPC.