Constant (programming)

From Wikipedia, the free encyclopedia

  (Redirected from Const)
Jump to: navigation, search

In computer programming, a constant is a special kind of variable whose value cannot be altered during program execution. Many programming languages make an explicit syntactic distinction between constant and variable symbols.

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).

Contents

[edit] In assembly language

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]

[edit] Types

Programming languages provide one of two kinds of constant variables:

[edit] Static

Languages such as Visual Basic allow assigning a fixed value to static constant (also known as a manifest 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.

[edit] Dynamic

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;.

[edit] const references

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.

[edit] Naming

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

[edit] References

  1. ^ IBM Systems Information. Instruction Set - Assembler Language Reference for PowerPC.
Personal tools