Constant (computer programming)

One very basic way is by simply writing a literal number, character, or string into the program code, which is straightforward in C, C++, and similar languages.

In assembly language, literal numbers and characters are done using the "immediate mode" instructions available on most microprocessors.

A preprocessor then replaces these names with the appropriate values before compiling, resulting in something functionally identical to using literals, with the speed advantages of immediate mode.

A global variable or static variable can be declared (or a symbol defined in assembly) with a keyword qualifier such as const, constant, or final, meaning that its value will be set at compile time and should not be changeable at runtime.

Memory protection can be applied to this area to prevent overwriting of such constants by errant pointers.

Compilers generally place a constant in a single memory location identified by symbol, rather than spread throughout the executable as with a macro.

[2] Besides the static constants described above, many procedural languages such as Ada and C++ extend the concept of constantness toward global variables that are created at initialization time, local variables that are automatically created at runtime on the stack or in registers, to dynamically allocated memory that is accessed by pointer, and to parameter lists in function headers.

Depending on the syntax, either a pointer or the object being pointed to may be constant, however normally the latter is desired.

Especially in C++ and C, the discipline of ensuring that the proper data structures are constant throughout the program is called const-correctness.

Declaring parameters as constants may be a way to signalise that this value should not be changed, but the programmer must keep in mind that checks about modification of an object cannot be done by the compiler.

[5] A constant data structure or object is referred to as "immutable" in object-oriented parlance.

Conversely, the mutable keyword allows a class member to be changed even if an object was instantiated as const.

Java has a qualifier called final that prevents changing a reference and makes sure it will never point to a different object.

The other, inheritance-inhibiting effect of Java's final when applied to methods and classes is induced in C# with the aid of the keyword sealed.

Const-correctness is an issue in imperative languages like C++ because by default name bindings typically create variables, which can vary, as the name suggests, and thus if one wishes to mark a binding as constant this requires some additional indication.

[b] In other programming language paradigms related issues arise, with some analogs to const-correctness found.

Such languages achieve the goals of const-correctness by default, drawing attention to modification rather than constantness.

In a number of object-oriented languages, there is the concept of an immutable object, which is particularly used for basic types like strings; notable examples include Java, JavaScript, Python, and C#.

Others use capitals and underscores for constants in a way similar to their traditional use for symbolic macros, such as SOME_CONSTANT.

One enforced convention is that in Ruby, any variable that begins with a capital letter is considered a constant, including class names.