Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant Object vs Immutable Object

Can I use the term "Constant Object" in the place of the term "Immutable Object"? Though I get the feeling that Immutable for an Object is what Constant is for a variable, I am not sure if this terminology is accepted. Please help me understand.

Thanks, Karthick S.

like image 845
Karthick S Avatar asked Feb 16 '11 17:02

Karthick S


People also ask

What is the difference between immutable and constant?

Immutable makes the contract that this object will not change, whatsoever (e.g. Python tuples, Java strings). Const makes the contract that in the scope of this variable it will not be modified (no promise whatsoever about what other threads might do to the object pointed to during this period, e.g. the C/C++ keyword).

Is an immutable variable a constant?

The difference is that constant variables can never be changed after compilation, while immutable variables can be set within the constructor. From the docs: State variables can be declared as constant or immutable. In both cases, the variables cannot be modified after the contract has been constructed.

What is the difference between immutable object and mutable object?

Objects whose value can change are said to be mutable. Objects whose value is unchangeable once they are created are called immutable.

Which is a immutable object?

An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code. Immutable objects are particularly useful in concurrent applications.


4 Answers

In fact, in Java the term constant has no defined meaning. It occurs in the JLS only in the larger term compile time constant expression, which is an expression which can (and must) be calculated by the compiler instead of at runtime. (And the keyword const is reserved to allow compilers to give better error messages.)

In Java, we instead use the term final to refer to variables (whether class, object or local ones) who can't be changed, and immutable when we refer to objects who can't change. Both can be used when speaking about a variable x - the first then means the variable itself (meaning it can't be switched to another object), the second means the object behind the variable. Thus here the two meanings are orthogonal, and are often combined to create a "true constant".

like image 113
Paŭlo Ebermann Avatar answered Sep 23 '22 15:09

Paŭlo Ebermann


I would read constant as being the same object (the same reference), whereas immutable clearly means to me the fact that the object doesn't change.

Since these are two different things, I would perhaps refer to this:

private final Immutable i = new Immutable();

as being a constant immutable object.

like image 38
Brian Agnew Avatar answered Sep 21 '22 15:09

Brian Agnew


Constant often has a very specific meaning in different programming languages. In java, a constant already refers to a constant variable, a variable that cannot be changed after assignment, e.g.:

final int FOO = 1;
FOO = 4; // constant variable cannot be changed.

An immutable object is a constant object in the sense that its properties can never be changed, but of course an object pointed to by a constant variable can still be changed. So to avoid confusion, the term immutable (which literally means "unchanging over time") is used.

like image 25
wds Avatar answered Sep 23 '22 15:09

wds


They are very close in meaning, with the understanding that an Object contains methods while a Constant is generally considered to only contain data.

Within Java, there's the additional consideration of the keyword final, which basically means non-reassignable. Some people will casually call a final variable a constant (as it's reference to a particular object is a constant. This often comes about due to confusion as to the particular roles of the member and the object it refers to, as 95% of the time a person does this to refer to an immutable Object.

Not every method is to return back data that depends wholly upon the internal members. For example System.currentTimeMillis() returns back a Unix like timestamp, yet there would be no need for the actual "System" object to change.

like image 39
Edwin Buck Avatar answered Sep 21 '22 15:09

Edwin Buck