A constant
data structure or object is referred to as "
immutable" in object-oriented parlance. An object being immutable confers some advantages in program design. For instance, it may be "copied" simply by copying its pointer or reference, avoiding a time-consuming copy operation and conserving memory. Object-oriented languages such as C++ extend constantness even further. Individual members of a struct or class may be made const even if the class is not. Conversely, the keyword allows a class member to be changed even if an object was instantiated as . This may be useful in cases where only data is meant to be , but some metadata or class-wide information may change. Even functions can be const in C++. The meaning here is that only a const function may be called for an object instantiated as const; a const function doesn't change any non-mutable data. C# has both a and a qualifier; its const is only for compile-time constants, while can be used in constructors and other runtime applications. In C++, there are four different kinds of : • , the traditional runtime constant • , a constant or expression which can be evaluated at compile time • , which declares that any call to a function must produce a value which is a compile-time expression • , which declares that the expression has static (constant) initialisation In
Rust, all declarations are constant unless declared , which makes them mutable. For compile-time constants, is used.
Java Java has a qualifier called that prevents changing a reference and makes sure it will never point to a different object. This does not prevent changes to the referred object itself. Java's is basically equivalent to a
pointer in C++. It does not provide the other features of . In
Java, the qualifier states that the affected data member or variable is not assignable, as below: final int i = 3; i = 4; // Error! Cannot modify a "final" object It must be decidable by the compilers where the variable with the marker is initialized, and it must be performed only once, or the class will not compile. Java's and C++'s keywords have the same meaning when applied with primitive variables. const int i = 3; // C++ declaration i = 4; // Error! Considering pointers, a reference in Java means something similar to pointer in C++. In C++, one can declare a "constant pointer type". Foo* const bar = mem_location; // const pointer type Here, must be initialised at the time of declaration and cannot be changed again, but what it points
is modifiable. I.e. is valid. It just can't point to another location. Final references in Java work the same way except that they can be declared uninitialized. final Foo i; // a Java declaration Note: Java does not support pointers. It is because
pointers (with restrictions) are the default way of accessing objects in Java, and Java does not use stars to indicate them. For example, in the last example is a pointer and can be used to access the instance. One can also declare a pointer to "read-only" data in C++. const Foo* bar; Here can be modified to point anything, anytime; just that pointed value cannot be modified
through pointer. There is no equivalent mechanism in Java. Thus there are also no methods. Const-correctness cannot be enforced in Java, although by use of interfaces and defining a read-only interface to the class and passing this around, one can ensure that objects can be passed around the system in a way that they cannot be modified.
Java collections framework provides a way to create an immutable wrapper of a via and similar methods. A method in Java can be declared "final", meaning that it cannot be overridden in subclasses. Java reserves the keyword, but it remains unused currently.
C# In
C#, the qualifier has the same effect on data members that does in Java and the does in C++; the modifier has an effect similar to that of in C/C++. The other, inheritance-inhibiting effect of Java's when applied to methods and classes is induced in C# with the aid of the keyword . Unlike C++, C# does not permit methods and parameters to be marked as . However one may also pass around read-only subclasses, and the
.NET Framework provides some support for converting mutable collections to immutable ones which may be passed as read-only wrappers. ==By paradigm==