[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the benefits of immutability

Perry E. Metzger wrote:

 > > The finality of the String class is a consequence of its
 > > immutability.
 > That's just plain incorrect. Obvious evidence is that there is also
 > a mutable string type and it is also final -- i.e. cannot be
 > subclassed. Just because the strings themselves are immutable
 > doesn't mean that you can't safely add extra methods etc. to a
 > subclass. The two are unconnected.

Generally speaking, it can be pretty hard to safely add methods to a
subclass without violating the Liskov substitutability principle.  A
classic example of this is the one cited by Joshua Bloch, which can be
found here:


(Search for "Don't use inheritance un-skeptically")

The problem is not that you can't add methods safely.  The problem is
that it is too easy to add them unsafely.  One of the String's stated
invariants is its guaranteed immutability.  The only way to make sure
this invariant is *never* violated is to prevent subclassing.  Thus,
true immutability also requires finality.

 > I think you miss the point.

I am sure I do.  I thought the point was that Java sucked.  I am not
going to argue with that.  I am just nitpicking on the specific
arguments brought up to back up the overarching suckiness claim.
Matter of fact, I am very interested to hear good arguments against
Java.  I have a small collection of those:

Java Critique
Java Is Not a Hairball
ASP better for Web Than Java
Java Design Flaws
(Checked) exceptions are bad
Java is a LFM