Permanent object versus immutable object

Can I use the term "Permanent Object" instead of the word "Immutable Object"? Although I get the feeling that Immutable for Object is a constant for a variable, I'm not sure that this terminology will be accepted. Please help me understand.

Thanks, Karthick S.

+8
java immutability
source share
5 answers

In fact, in Java, the term constant does not have a specific meaning. This occurs in JLS only in a broader expression, an expression for the compile time constant , which is an expression that can (and should be) computed, and not at run time. (And the const keyword is reserved to allow compilers to give more efficient error messages.)

Instead, in Java, we use the term final to refer to variables (whether cool, object, or local) that cannot be changed and are immutable when we refer to objects that cannot change. Both can be used when it comes to the variable x - the first means that the variable itself (which means that it cannot be switched to another object), the second means the object behind the variable. Thus, here the two values ​​are orthogonal and are often combined to create a “true constant”.

+10
source share

I would read the constant as one and the same object (same reference), while immutable clearly means to me the fact that the object does not change.

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

 private final Immutable i = new Immutable(); 

as a constant immutable object.

+4
source share

They are very close in meaning with the understanding that the object contains methods, while a constant is usually considered to contain only data.

Inside Java, there is an additional look at the final keyword, which basically means non-tunable. Some people accidentally call the final variable a constant (since a reference to a specific object is a constant, which often arises from confusion regarding the specific roles of the member and the object to which it refers, as 95% of the time a person does this, referring to an immutable object.

Not every method should return data that is completely dependent on internal members. For example, System.currentTimeMillis() returns the Unix timestamp, but does not require the actual "System" object to change.

+1
source share

A 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, for example:

 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, the object pointed to by a constant variable can still be changed. Therefore, to avoid confusion, the term immutable (which literally means “immutable over time”) is used.

+1
source share

The immutability of an object means that it cannot transform its state ... i.e. can't mutate ... For reference

 final class Person { private int age = 0; public Person(int age) { this.age = age; } } 

Objects of this type are immutable objects, since you cannot change its state .... (forget Reflection for a moment)

A constant, on the other hand, in programming means built-in ... Even the compiler does that they build constant variable values ​​for primitive types ... for object types, this means that the ref variable cannot be reassigned.

-2
source share

All Articles