L
Lew
Öö Tiib said:Seems that "Favor Immutability" is not that popular idiom. Google for
It's one of the most fundamental idioms in Java.
"Const Correctness" 39,000 results, "Favor Immutability" 1,080. It is
That's a measure of how often a particular phrase turns up, not how often the
topic comes up. Your "research' is a complete red herring.
more about how to create immutable classes, while in C++ you do not
need to (but may) make special classes for constant instances.
Yeesh. "How to create immutable classes" *is* a discussion of "favor
immutability". How come you didn't include those stats in your count? Hm?
The keywords differ in how they're applied.
Maybe Java doesn't use a fancy term; when discussing immutability and final we
just say, "making things final" or "making things immutable" or other phrases
with the word "final" and "immutable", and everyone understands that we talk
about that because of the advice to "favor immutability" without having to
quote that particular chapter title.
No one lasts long in Java without hearing about Joshua Bloch's /Effective
Java/, whence came the phrase "favor immutability", and no one progresses far
as a Java programmer without studying that book.
So strengthen your google-fu and try again, and don't quote misleading and
irrelevant statistics.
Öö Tiib said:Huh? But these are tools, language features. Not mandatory to use
always but most like them since these really help. Perhaps that
'final' is simply considered enough by authors of java [sic]. There are fine
languages like Python with even less features supporting
immutability.
As we've established elsethread, 'final' and 'const' are somewhat similar in
certain respects, but not the same really. They fill a niche of immutability
in different languages with different ecologies, and do so at somewhat
different levels.
I stand corrected (thanks, Pete) on any implication that the keywords are
highly similar. I never intended to imply that. All I'm saying is that they
have a similarity in purpose, to render elements immutable.
Öö Tiib said:Ok. Making primitive immutable with 'final' is yes in certain ways
same. It is mostly perhaps used for naming single constant values, not
write protection? You can also make a pointer immutable with 'final'.
That is sometimes done in C++ too but usually one uses a reference for
it ('const' is not needed at all).
Java doesn't have C++ references; what Java calls "reference" is actually a
pointer.
There similarities end. 'final' about member function means that it
can not be further overridden and 'final' about class means that it
can not be derived from. These are entirely different purposes than
I was only referring to the use of 'final' for variables. Method and class
names already cannot be changed to point to something different.
'const' has. It feels like 'final' is for some sort of global and meta-
immutability when 'const' is more for selectively write-protecting
(possibly mutable) data.
'final' serves that purpose in Java.
Everyone here seems to think that Java 'final' is just nothing a-'tall like
C[++] 'const'. They are, of course, mistaken.
Öö Tiib said:'const' yes, feels more oriented for not giving (and taking) unneeded
privileges locally. Major feature of 'const' is to declare that code
will not modify the object pointed to by a pointer through that
pointer. Other feature is to declare that function does not modify its
parameter and third is to show that member function does not modify
the object. How you do any of these with 'final'?
Java cannot access an object by a pointer to a pointer, since those don't even
exist, so that purpose isn't needed. 'final' applied to a function parameter
has a cognate purpose - the function cannot change the contents of the
parameter. To prevent anything (not just a member function) from modifying
the object, Java uses 'final', but on the attributes of the object, not the
method.
Öö Tiib said:I am not saying that different feature set makes one language better
Nor am I.
than other, just that 'final's purpose feels different and such
The purpose is similar, it's the mechanics that differ.
comparison feels stretched out, nothing to do. You can have immutable
view of mutable data by writing special interface in java [sic], but the
Java interfaces cannot enforce immutability.
cost is higher than with 'const' and what it all has to do with
'final'?
Immutability in Java is enforced with 'final', as immutability in C++ is with
'const'. Yes, there are differences, of course. They aren't the same. But
given the different ecologies, they have a (sort of) similar purpose. That's
what.